ib_uverbs_main.c revision 331769
1/* 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. 7 * 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: 13 * 14 * Redistribution and use in source and binary forms, with or 15 * without modification, are permitted provided that the following 16 * conditions are met: 17 * 18 * - Redistributions of source code must retain the above 19 * copyright notice, this list of conditions and the following 20 * disclaimer. 21 * 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. 26 * 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 34 * SOFTWARE. 35 */ 36 37#include <linux/module.h> 38#include <linux/device.h> 39#include <linux/err.h> 40#include <linux/fs.h> 41#include <linux/poll.h> 42#include <linux/sched.h> 43#include <linux/file.h> 44#include <linux/cdev.h> 45#include <linux/slab.h> 46#include <linux/pci.h> 47 48#include <asm/uaccess.h> 49 50#include <rdma/ib.h> 51 52#include "uverbs.h" 53 54MODULE_AUTHOR("Roland Dreier"); 55MODULE_DESCRIPTION("InfiniBand userspace verbs access"); 56MODULE_LICENSE("Dual BSD/GPL"); 57 58enum { 59 IB_UVERBS_MAJOR = 231, 60 IB_UVERBS_BASE_MINOR = 192, 61 IB_UVERBS_MAX_DEVICES = 32 62}; 63 64#define IB_UVERBS_BASE_DEV MKDEV(IB_UVERBS_MAJOR, IB_UVERBS_BASE_MINOR) 65 66static struct class *uverbs_class; 67 68DEFINE_SPINLOCK(ib_uverbs_idr_lock); 69DEFINE_IDR(ib_uverbs_pd_idr); 70DEFINE_IDR(ib_uverbs_mr_idr); 71DEFINE_IDR(ib_uverbs_mw_idr); 72DEFINE_IDR(ib_uverbs_ah_idr); 73DEFINE_IDR(ib_uverbs_cq_idr); 74DEFINE_IDR(ib_uverbs_qp_idr); 75DEFINE_IDR(ib_uverbs_srq_idr); 76DEFINE_IDR(ib_uverbs_xrcd_idr); 77DEFINE_IDR(ib_uverbs_rule_idr); 78DEFINE_IDR(ib_uverbs_wq_idr); 79DEFINE_IDR(ib_uverbs_rwq_ind_tbl_idr); 80 81static DEFINE_SPINLOCK(map_lock); 82static DECLARE_BITMAP(dev_map, IB_UVERBS_MAX_DEVICES); 83 84static ssize_t (*uverbs_cmd_table[])(struct ib_uverbs_file *file, 85 struct ib_device *ib_dev, 86 const char __user *buf, int in_len, 87 int out_len) = { 88 [IB_USER_VERBS_CMD_GET_CONTEXT] = ib_uverbs_get_context, 89 [IB_USER_VERBS_CMD_QUERY_DEVICE] = ib_uverbs_query_device, 90 [IB_USER_VERBS_CMD_QUERY_PORT] = ib_uverbs_query_port, 91 [IB_USER_VERBS_CMD_ALLOC_PD] = ib_uverbs_alloc_pd, 92 [IB_USER_VERBS_CMD_DEALLOC_PD] = ib_uverbs_dealloc_pd, 93 [IB_USER_VERBS_CMD_REG_MR] = ib_uverbs_reg_mr, 94 [IB_USER_VERBS_CMD_REREG_MR] = ib_uverbs_rereg_mr, 95 [IB_USER_VERBS_CMD_DEREG_MR] = ib_uverbs_dereg_mr, 96 [IB_USER_VERBS_CMD_ALLOC_MW] = ib_uverbs_alloc_mw, 97 [IB_USER_VERBS_CMD_DEALLOC_MW] = ib_uverbs_dealloc_mw, 98 [IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL] = ib_uverbs_create_comp_channel, 99 [IB_USER_VERBS_CMD_CREATE_CQ] = ib_uverbs_create_cq, 100 [IB_USER_VERBS_CMD_RESIZE_CQ] = ib_uverbs_resize_cq, 101 [IB_USER_VERBS_CMD_POLL_CQ] = ib_uverbs_poll_cq, 102 [IB_USER_VERBS_CMD_REQ_NOTIFY_CQ] = ib_uverbs_req_notify_cq, 103 [IB_USER_VERBS_CMD_DESTROY_CQ] = ib_uverbs_destroy_cq, 104 [IB_USER_VERBS_CMD_CREATE_QP] = ib_uverbs_create_qp, 105 [IB_USER_VERBS_CMD_QUERY_QP] = ib_uverbs_query_qp, 106 [IB_USER_VERBS_CMD_MODIFY_QP] = ib_uverbs_modify_qp, 107 [IB_USER_VERBS_CMD_DESTROY_QP] = ib_uverbs_destroy_qp, 108 [IB_USER_VERBS_CMD_POST_SEND] = ib_uverbs_post_send, 109 [IB_USER_VERBS_CMD_POST_RECV] = ib_uverbs_post_recv, 110 [IB_USER_VERBS_CMD_POST_SRQ_RECV] = ib_uverbs_post_srq_recv, 111 [IB_USER_VERBS_CMD_CREATE_AH] = ib_uverbs_create_ah, 112 [IB_USER_VERBS_CMD_DESTROY_AH] = ib_uverbs_destroy_ah, 113 [IB_USER_VERBS_CMD_ATTACH_MCAST] = ib_uverbs_attach_mcast, 114 [IB_USER_VERBS_CMD_DETACH_MCAST] = ib_uverbs_detach_mcast, 115 [IB_USER_VERBS_CMD_CREATE_SRQ] = ib_uverbs_create_srq, 116 [IB_USER_VERBS_CMD_MODIFY_SRQ] = ib_uverbs_modify_srq, 117 [IB_USER_VERBS_CMD_QUERY_SRQ] = ib_uverbs_query_srq, 118 [IB_USER_VERBS_CMD_DESTROY_SRQ] = ib_uverbs_destroy_srq, 119 [IB_USER_VERBS_CMD_OPEN_XRCD] = ib_uverbs_open_xrcd, 120 [IB_USER_VERBS_CMD_CLOSE_XRCD] = ib_uverbs_close_xrcd, 121 [IB_USER_VERBS_CMD_CREATE_XSRQ] = ib_uverbs_create_xsrq, 122 [IB_USER_VERBS_CMD_OPEN_QP] = ib_uverbs_open_qp, 123}; 124 125static int (*uverbs_ex_cmd_table[])(struct ib_uverbs_file *file, 126 struct ib_device *ib_dev, 127 struct ib_udata *ucore, 128 struct ib_udata *uhw) = { 129 [IB_USER_VERBS_EX_CMD_CREATE_FLOW] = ib_uverbs_ex_create_flow, 130 [IB_USER_VERBS_EX_CMD_DESTROY_FLOW] = ib_uverbs_ex_destroy_flow, 131 [IB_USER_VERBS_EX_CMD_QUERY_DEVICE] = ib_uverbs_ex_query_device, 132 [IB_USER_VERBS_EX_CMD_CREATE_CQ] = ib_uverbs_ex_create_cq, 133 [IB_USER_VERBS_EX_CMD_CREATE_QP] = ib_uverbs_ex_create_qp, 134 [IB_USER_VERBS_EX_CMD_CREATE_WQ] = ib_uverbs_ex_create_wq, 135 [IB_USER_VERBS_EX_CMD_MODIFY_WQ] = ib_uverbs_ex_modify_wq, 136 [IB_USER_VERBS_EX_CMD_DESTROY_WQ] = ib_uverbs_ex_destroy_wq, 137 [IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL] = ib_uverbs_ex_create_rwq_ind_table, 138 [IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL] = ib_uverbs_ex_destroy_rwq_ind_table, 139}; 140 141static void ib_uverbs_add_one(struct ib_device *device); 142static void ib_uverbs_remove_one(struct ib_device *device, void *client_data); 143 144int uverbs_dealloc_mw(struct ib_mw *mw) 145{ 146 struct ib_pd *pd = mw->pd; 147 int ret; 148 149 ret = mw->device->dealloc_mw(mw); 150 if (!ret) 151 atomic_dec(&pd->usecnt); 152 return ret; 153} 154 155static void ib_uverbs_release_dev(struct kobject *kobj) 156{ 157 struct ib_uverbs_device *dev = 158 container_of(kobj, struct ib_uverbs_device, kobj); 159 160 cleanup_srcu_struct(&dev->disassociate_srcu); 161 kfree(dev); 162} 163 164static struct kobj_type ib_uverbs_dev_ktype = { 165 .release = ib_uverbs_release_dev, 166}; 167 168static void ib_uverbs_release_event_file(struct kref *ref) 169{ 170 struct ib_uverbs_event_file *file = 171 container_of(ref, struct ib_uverbs_event_file, ref); 172 173 kfree(file); 174} 175 176void ib_uverbs_release_ucq(struct ib_uverbs_file *file, 177 struct ib_uverbs_event_file *ev_file, 178 struct ib_ucq_object *uobj) 179{ 180 struct ib_uverbs_event *evt, *tmp; 181 182 if (ev_file) { 183 spin_lock_irq(&ev_file->lock); 184 list_for_each_entry_safe(evt, tmp, &uobj->comp_list, obj_list) { 185 list_del(&evt->list); 186 kfree(evt); 187 } 188 spin_unlock_irq(&ev_file->lock); 189 190 kref_put(&ev_file->ref, ib_uverbs_release_event_file); 191 } 192 193 spin_lock_irq(&file->async_file->lock); 194 list_for_each_entry_safe(evt, tmp, &uobj->async_list, obj_list) { 195 list_del(&evt->list); 196 kfree(evt); 197 } 198 spin_unlock_irq(&file->async_file->lock); 199} 200 201void ib_uverbs_release_uevent(struct ib_uverbs_file *file, 202 struct ib_uevent_object *uobj) 203{ 204 struct ib_uverbs_event *evt, *tmp; 205 206 spin_lock_irq(&file->async_file->lock); 207 list_for_each_entry_safe(evt, tmp, &uobj->event_list, obj_list) { 208 list_del(&evt->list); 209 kfree(evt); 210 } 211 spin_unlock_irq(&file->async_file->lock); 212} 213 214static void ib_uverbs_detach_umcast(struct ib_qp *qp, 215 struct ib_uqp_object *uobj) 216{ 217 struct ib_uverbs_mcast_entry *mcast, *tmp; 218 219 list_for_each_entry_safe(mcast, tmp, &uobj->mcast_list, list) { 220 ib_detach_mcast(qp, &mcast->gid, mcast->lid); 221 list_del(&mcast->list); 222 kfree(mcast); 223 } 224} 225 226static int ib_uverbs_cleanup_ucontext(struct ib_uverbs_file *file, 227 struct ib_ucontext *context) 228{ 229 struct ib_uobject *uobj, *tmp; 230 231 context->closing = 1; 232 233 list_for_each_entry_safe(uobj, tmp, &context->ah_list, list) { 234 struct ib_ah *ah = uobj->object; 235 236 idr_remove_uobj(&ib_uverbs_ah_idr, uobj); 237 ib_destroy_ah(ah); 238 kfree(uobj); 239 } 240 241 /* Remove MWs before QPs, in order to support type 2A MWs. */ 242 list_for_each_entry_safe(uobj, tmp, &context->mw_list, list) { 243 struct ib_mw *mw = uobj->object; 244 245 idr_remove_uobj(&ib_uverbs_mw_idr, uobj); 246 uverbs_dealloc_mw(mw); 247 kfree(uobj); 248 } 249 250 list_for_each_entry_safe(uobj, tmp, &context->rule_list, list) { 251 struct ib_flow *flow_id = uobj->object; 252 253 idr_remove_uobj(&ib_uverbs_rule_idr, uobj); 254 ib_destroy_flow(flow_id); 255 kfree(uobj); 256 } 257 258 list_for_each_entry_safe(uobj, tmp, &context->qp_list, list) { 259 struct ib_qp *qp = uobj->object; 260 struct ib_uqp_object *uqp = 261 container_of(uobj, struct ib_uqp_object, uevent.uobject); 262 263 idr_remove_uobj(&ib_uverbs_qp_idr, uobj); 264 if (qp == qp->real_qp) 265 ib_uverbs_detach_umcast(qp, uqp); 266 ib_destroy_qp(qp); 267 ib_uverbs_release_uevent(file, &uqp->uevent); 268 kfree(uqp); 269 } 270 271 list_for_each_entry_safe(uobj, tmp, &context->rwq_ind_tbl_list, list) { 272 struct ib_rwq_ind_table *rwq_ind_tbl = uobj->object; 273 struct ib_wq **ind_tbl = rwq_ind_tbl->ind_tbl; 274 275 idr_remove_uobj(&ib_uverbs_rwq_ind_tbl_idr, uobj); 276 ib_destroy_rwq_ind_table(rwq_ind_tbl); 277 kfree(ind_tbl); 278 kfree(uobj); 279 } 280 281 list_for_each_entry_safe(uobj, tmp, &context->wq_list, list) { 282 struct ib_wq *wq = uobj->object; 283 struct ib_uwq_object *uwq = 284 container_of(uobj, struct ib_uwq_object, uevent.uobject); 285 286 idr_remove_uobj(&ib_uverbs_wq_idr, uobj); 287 ib_destroy_wq(wq); 288 ib_uverbs_release_uevent(file, &uwq->uevent); 289 kfree(uwq); 290 } 291 292 list_for_each_entry_safe(uobj, tmp, &context->srq_list, list) { 293 struct ib_srq *srq = uobj->object; 294 struct ib_uevent_object *uevent = 295 container_of(uobj, struct ib_uevent_object, uobject); 296 297 idr_remove_uobj(&ib_uverbs_srq_idr, uobj); 298 ib_destroy_srq(srq); 299 ib_uverbs_release_uevent(file, uevent); 300 kfree(uevent); 301 } 302 303 list_for_each_entry_safe(uobj, tmp, &context->cq_list, list) { 304 struct ib_cq *cq = uobj->object; 305 struct ib_uverbs_event_file *ev_file = cq->cq_context; 306 struct ib_ucq_object *ucq = 307 container_of(uobj, struct ib_ucq_object, uobject); 308 309 idr_remove_uobj(&ib_uverbs_cq_idr, uobj); 310 ib_destroy_cq(cq); 311 ib_uverbs_release_ucq(file, ev_file, ucq); 312 kfree(ucq); 313 } 314 315 list_for_each_entry_safe(uobj, tmp, &context->mr_list, list) { 316 struct ib_mr *mr = uobj->object; 317 318 idr_remove_uobj(&ib_uverbs_mr_idr, uobj); 319 ib_dereg_mr(mr); 320 kfree(uobj); 321 } 322 323 mutex_lock(&file->device->xrcd_tree_mutex); 324 list_for_each_entry_safe(uobj, tmp, &context->xrcd_list, list) { 325 struct ib_xrcd *xrcd = uobj->object; 326 struct ib_uxrcd_object *uxrcd = 327 container_of(uobj, struct ib_uxrcd_object, uobject); 328 329 idr_remove_uobj(&ib_uverbs_xrcd_idr, uobj); 330 ib_uverbs_dealloc_xrcd(file->device, xrcd); 331 kfree(uxrcd); 332 } 333 mutex_unlock(&file->device->xrcd_tree_mutex); 334 335 list_for_each_entry_safe(uobj, tmp, &context->pd_list, list) { 336 struct ib_pd *pd = uobj->object; 337 338 idr_remove_uobj(&ib_uverbs_pd_idr, uobj); 339 ib_dealloc_pd(pd); 340 kfree(uobj); 341 } 342 343 put_pid(context->tgid); 344 345 return context->device->dealloc_ucontext(context); 346} 347 348static void ib_uverbs_comp_dev(struct ib_uverbs_device *dev) 349{ 350 complete(&dev->comp); 351} 352 353static void ib_uverbs_release_file(struct kref *ref) 354{ 355 struct ib_uverbs_file *file = 356 container_of(ref, struct ib_uverbs_file, ref); 357 struct ib_device *ib_dev; 358 int srcu_key; 359 360 srcu_key = srcu_read_lock(&file->device->disassociate_srcu); 361 ib_dev = srcu_dereference(file->device->ib_dev, 362 &file->device->disassociate_srcu); 363 if (ib_dev && !ib_dev->disassociate_ucontext) 364 module_put(ib_dev->owner); 365 srcu_read_unlock(&file->device->disassociate_srcu, srcu_key); 366 367 if (atomic_dec_and_test(&file->device->refcount)) 368 ib_uverbs_comp_dev(file->device); 369 370 kfree(file); 371} 372 373static ssize_t ib_uverbs_event_read(struct file *filp, char __user *buf, 374 size_t count, loff_t *pos) 375{ 376 struct ib_uverbs_event_file *file = filp->private_data; 377 struct ib_uverbs_event *event; 378 int eventsz; 379 int ret = 0; 380 381 spin_lock_irq(&file->lock); 382 383 while (list_empty(&file->event_list)) { 384 spin_unlock_irq(&file->lock); 385 386 if (filp->f_flags & O_NONBLOCK) 387 return -EAGAIN; 388 389 if (wait_event_interruptible(file->poll_wait, 390 (!list_empty(&file->event_list) || 391 /* The barriers built into wait_event_interruptible() 392 * and wake_up() guarentee this will see the null set 393 * without using RCU 394 */ 395 !file->uverbs_file->device->ib_dev))) 396 return -ERESTARTSYS; 397 398 /* If device was disassociated and no event exists set an error */ 399 if (list_empty(&file->event_list) && 400 !file->uverbs_file->device->ib_dev) 401 return -EIO; 402 403 spin_lock_irq(&file->lock); 404 } 405 406 event = list_entry(file->event_list.next, struct ib_uverbs_event, list); 407 408 if (file->is_async) 409 eventsz = sizeof (struct ib_uverbs_async_event_desc); 410 else 411 eventsz = sizeof (struct ib_uverbs_comp_event_desc); 412 413 if (eventsz > count) { 414 ret = -EINVAL; 415 event = NULL; 416 } else { 417 list_del(file->event_list.next); 418 if (event->counter) { 419 ++(*event->counter); 420 list_del(&event->obj_list); 421 } 422 } 423 424 spin_unlock_irq(&file->lock); 425 426 if (event) { 427 if (copy_to_user(buf, event, eventsz)) 428 ret = -EFAULT; 429 else 430 ret = eventsz; 431 } 432 433 kfree(event); 434 435 return ret; 436} 437 438static unsigned int ib_uverbs_event_poll(struct file *filp, 439 struct poll_table_struct *wait) 440{ 441 unsigned int pollflags = 0; 442 struct ib_uverbs_event_file *file = filp->private_data; 443 444 poll_wait(filp, &file->poll_wait, wait); 445 446 spin_lock_irq(&file->lock); 447 if (!list_empty(&file->event_list)) 448 pollflags = POLLIN | POLLRDNORM; 449 spin_unlock_irq(&file->lock); 450 451 return pollflags; 452} 453 454static int ib_uverbs_event_fasync(int fd, struct file *filp, int on) 455{ 456 struct ib_uverbs_event_file *file = filp->private_data; 457 458 return fasync_helper(fd, filp, on, &file->async_queue); 459} 460 461static int ib_uverbs_event_close(struct inode *inode, struct file *filp) 462{ 463 struct ib_uverbs_event_file *file = filp->private_data; 464 struct ib_uverbs_event *entry, *tmp; 465 int closed_already = 0; 466 467 mutex_lock(&file->uverbs_file->device->lists_mutex); 468 spin_lock_irq(&file->lock); 469 closed_already = file->is_closed; 470 file->is_closed = 1; 471 list_for_each_entry_safe(entry, tmp, &file->event_list, list) { 472 if (entry->counter) 473 list_del(&entry->obj_list); 474 kfree(entry); 475 } 476 spin_unlock_irq(&file->lock); 477 if (!closed_already) { 478 list_del(&file->list); 479 if (file->is_async) 480 ib_unregister_event_handler(&file->uverbs_file-> 481 event_handler); 482 } 483 mutex_unlock(&file->uverbs_file->device->lists_mutex); 484 485 kref_put(&file->uverbs_file->ref, ib_uverbs_release_file); 486 kref_put(&file->ref, ib_uverbs_release_event_file); 487 488 return 0; 489} 490 491static const struct file_operations uverbs_event_fops = { 492 .owner = THIS_MODULE, 493 .read = ib_uverbs_event_read, 494 .poll = ib_uverbs_event_poll, 495 .release = ib_uverbs_event_close, 496 .fasync = ib_uverbs_event_fasync, 497 .llseek = no_llseek, 498}; 499 500void ib_uverbs_comp_handler(struct ib_cq *cq, void *cq_context) 501{ 502 struct ib_uverbs_event_file *file = cq_context; 503 struct ib_ucq_object *uobj; 504 struct ib_uverbs_event *entry; 505 unsigned long flags; 506 507 if (!file) 508 return; 509 510 spin_lock_irqsave(&file->lock, flags); 511 if (file->is_closed) { 512 spin_unlock_irqrestore(&file->lock, flags); 513 return; 514 } 515 516 entry = kmalloc(sizeof *entry, GFP_ATOMIC); 517 if (!entry) { 518 spin_unlock_irqrestore(&file->lock, flags); 519 return; 520 } 521 522 uobj = container_of(cq->uobject, struct ib_ucq_object, uobject); 523 524 entry->desc.comp.cq_handle = cq->uobject->user_handle; 525 entry->counter = &uobj->comp_events_reported; 526 527 list_add_tail(&entry->list, &file->event_list); 528 list_add_tail(&entry->obj_list, &uobj->comp_list); 529 spin_unlock_irqrestore(&file->lock, flags); 530 531 wake_up_interruptible(&file->poll_wait); 532 kill_fasync(&file->async_queue, SIGIO, POLL_IN); 533} 534 535static void ib_uverbs_async_handler(struct ib_uverbs_file *file, 536 __u64 element, __u64 event, 537 struct list_head *obj_list, 538 u32 *counter) 539{ 540 struct ib_uverbs_event *entry; 541 unsigned long flags; 542 543 spin_lock_irqsave(&file->async_file->lock, flags); 544 if (file->async_file->is_closed) { 545 spin_unlock_irqrestore(&file->async_file->lock, flags); 546 return; 547 } 548 549 entry = kmalloc(sizeof *entry, GFP_ATOMIC); 550 if (!entry) { 551 spin_unlock_irqrestore(&file->async_file->lock, flags); 552 return; 553 } 554 555 entry->desc.async.element = element; 556 entry->desc.async.event_type = event; 557 entry->desc.async.reserved = 0; 558 entry->counter = counter; 559 560 list_add_tail(&entry->list, &file->async_file->event_list); 561 if (obj_list) 562 list_add_tail(&entry->obj_list, obj_list); 563 spin_unlock_irqrestore(&file->async_file->lock, flags); 564 565 wake_up_interruptible(&file->async_file->poll_wait); 566 kill_fasync(&file->async_file->async_queue, SIGIO, POLL_IN); 567} 568 569void ib_uverbs_cq_event_handler(struct ib_event *event, void *context_ptr) 570{ 571 struct ib_ucq_object *uobj = container_of(event->element.cq->uobject, 572 struct ib_ucq_object, uobject); 573 574 ib_uverbs_async_handler(uobj->uverbs_file, uobj->uobject.user_handle, 575 event->event, &uobj->async_list, 576 &uobj->async_events_reported); 577} 578 579void ib_uverbs_qp_event_handler(struct ib_event *event, void *context_ptr) 580{ 581 struct ib_uevent_object *uobj; 582 583 /* for XRC target qp's, check that qp is live */ 584 if (!event->element.qp->uobject || !event->element.qp->uobject->live) 585 return; 586 587 uobj = container_of(event->element.qp->uobject, 588 struct ib_uevent_object, uobject); 589 590 ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle, 591 event->event, &uobj->event_list, 592 &uobj->events_reported); 593} 594 595void ib_uverbs_wq_event_handler(struct ib_event *event, void *context_ptr) 596{ 597 struct ib_uevent_object *uobj = container_of(event->element.wq->uobject, 598 struct ib_uevent_object, uobject); 599 600 ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle, 601 event->event, &uobj->event_list, 602 &uobj->events_reported); 603} 604 605void ib_uverbs_srq_event_handler(struct ib_event *event, void *context_ptr) 606{ 607 struct ib_uevent_object *uobj; 608 609 uobj = container_of(event->element.srq->uobject, 610 struct ib_uevent_object, uobject); 611 612 ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle, 613 event->event, &uobj->event_list, 614 &uobj->events_reported); 615} 616 617void ib_uverbs_event_handler(struct ib_event_handler *handler, 618 struct ib_event *event) 619{ 620 struct ib_uverbs_file *file = 621 container_of(handler, struct ib_uverbs_file, event_handler); 622 623 ib_uverbs_async_handler(file, event->element.port_num, event->event, 624 NULL, NULL); 625} 626 627void ib_uverbs_free_async_event_file(struct ib_uverbs_file *file) 628{ 629 kref_put(&file->async_file->ref, ib_uverbs_release_event_file); 630 file->async_file = NULL; 631} 632 633struct file *ib_uverbs_alloc_event_file(struct ib_uverbs_file *uverbs_file, 634 struct ib_device *ib_dev, 635 int is_async) 636{ 637 struct ib_uverbs_event_file *ev_file; 638 struct file *filp; 639 int ret; 640 641 ev_file = kzalloc(sizeof(*ev_file), GFP_KERNEL); 642 if (!ev_file) 643 return ERR_PTR(-ENOMEM); 644 645 kref_init(&ev_file->ref); 646 spin_lock_init(&ev_file->lock); 647 INIT_LIST_HEAD(&ev_file->event_list); 648 init_waitqueue_head(&ev_file->poll_wait); 649 ev_file->uverbs_file = uverbs_file; 650 kref_get(&ev_file->uverbs_file->ref); 651 ev_file->async_queue = NULL; 652 ev_file->is_closed = 0; 653 654 /* 655 * fops_get() can't fail here, because we're coming from a 656 * system call on a uverbs file, which will already have a 657 * module reference. 658 */ 659 filp = alloc_file(FMODE_READ, fops_get(&uverbs_event_fops)); 660 if (IS_ERR(filp)) 661 goto err_put_refs; 662 filp->private_data = ev_file; 663 664 mutex_lock(&uverbs_file->device->lists_mutex); 665 list_add_tail(&ev_file->list, 666 &uverbs_file->device->uverbs_events_file_list); 667 mutex_unlock(&uverbs_file->device->lists_mutex); 668 669 if (is_async) { 670 WARN_ON(uverbs_file->async_file); 671 uverbs_file->async_file = ev_file; 672 kref_get(&uverbs_file->async_file->ref); 673 INIT_IB_EVENT_HANDLER(&uverbs_file->event_handler, 674 ib_dev, 675 ib_uverbs_event_handler); 676 ret = ib_register_event_handler(&uverbs_file->event_handler); 677 if (ret) 678 goto err_put_file; 679 680 /* At that point async file stuff was fully set */ 681 ev_file->is_async = 1; 682 } 683 684 return filp; 685 686err_put_file: 687 fput(filp); 688 kref_put(&uverbs_file->async_file->ref, ib_uverbs_release_event_file); 689 uverbs_file->async_file = NULL; 690 return ERR_PTR(ret); 691 692err_put_refs: 693 kref_put(&ev_file->uverbs_file->ref, ib_uverbs_release_file); 694 kref_put(&ev_file->ref, ib_uverbs_release_event_file); 695 return filp; 696} 697 698/* 699 * Look up a completion event file by FD. If lookup is successful, 700 * takes a ref to the event file struct that it returns; if 701 * unsuccessful, returns NULL. 702 */ 703struct ib_uverbs_event_file *ib_uverbs_lookup_comp_file(int fd) 704{ 705 struct ib_uverbs_event_file *ev_file = NULL; 706 struct fd f = fdget(fd); 707 708 if (!f.file) 709 return NULL; 710 711 if (f.file->f_op != &uverbs_event_fops) 712 goto out; 713 714 ev_file = f.file->private_data; 715 if (ev_file->is_async) { 716 ev_file = NULL; 717 goto out; 718 } 719 720 kref_get(&ev_file->ref); 721 722out: 723 fdput(f); 724 return ev_file; 725} 726 727static int verify_command_mask(struct ib_device *ib_dev, __u32 command) 728{ 729 u64 mask; 730 731 if (command <= IB_USER_VERBS_CMD_OPEN_QP) 732 mask = ib_dev->uverbs_cmd_mask; 733 else 734 mask = ib_dev->uverbs_ex_cmd_mask; 735 736 if (mask & ((u64)1 << command)) 737 return 0; 738 739 return -1; 740} 741 742static ssize_t ib_uverbs_write(struct file *filp, const char __user *buf, 743 size_t count, loff_t *pos) 744{ 745 struct ib_uverbs_file *file = filp->private_data; 746 struct ib_device *ib_dev; 747 struct ib_uverbs_cmd_hdr hdr; 748 __u32 command; 749 __u32 flags; 750 int srcu_key; 751 ssize_t ret; 752 753 if (WARN_ON_ONCE(!ib_safe_file_access(filp))) 754 return -EACCES; 755 756 if (count < sizeof hdr) 757 return -EINVAL; 758 759 if (copy_from_user(&hdr, buf, sizeof hdr)) 760 return -EFAULT; 761 762 srcu_key = srcu_read_lock(&file->device->disassociate_srcu); 763 ib_dev = srcu_dereference(file->device->ib_dev, 764 &file->device->disassociate_srcu); 765 if (!ib_dev) { 766 ret = -EIO; 767 goto out; 768 } 769 770 if (hdr.command & ~(__u32)(IB_USER_VERBS_CMD_FLAGS_MASK | 771 IB_USER_VERBS_CMD_COMMAND_MASK)) { 772 ret = -EINVAL; 773 goto out; 774 } 775 776 command = hdr.command & IB_USER_VERBS_CMD_COMMAND_MASK; 777 if (verify_command_mask(ib_dev, command)) { 778 ret = -EOPNOTSUPP; 779 goto out; 780 } 781 782 if (!file->ucontext && 783 command != IB_USER_VERBS_CMD_GET_CONTEXT) { 784 ret = -EINVAL; 785 goto out; 786 } 787 788 flags = (hdr.command & 789 IB_USER_VERBS_CMD_FLAGS_MASK) >> IB_USER_VERBS_CMD_FLAGS_SHIFT; 790 791 if (!flags) { 792 if (command >= ARRAY_SIZE(uverbs_cmd_table) || 793 !uverbs_cmd_table[command]) { 794 ret = -EINVAL; 795 goto out; 796 } 797 798 if (hdr.in_words * 4 != count) { 799 ret = -EINVAL; 800 goto out; 801 } 802 803 ret = uverbs_cmd_table[command](file, ib_dev, 804 buf + sizeof(hdr), 805 hdr.in_words * 4, 806 hdr.out_words * 4); 807 808 } else if (flags == IB_USER_VERBS_CMD_FLAG_EXTENDED) { 809 struct ib_uverbs_ex_cmd_hdr ex_hdr; 810 struct ib_udata ucore; 811 struct ib_udata uhw; 812 size_t written_count = count; 813 814 if (command >= ARRAY_SIZE(uverbs_ex_cmd_table) || 815 !uverbs_ex_cmd_table[command]) { 816 ret = -ENOSYS; 817 goto out; 818 } 819 820 if (!file->ucontext) { 821 ret = -EINVAL; 822 goto out; 823 } 824 825 if (count < (sizeof(hdr) + sizeof(ex_hdr))) { 826 ret = -EINVAL; 827 goto out; 828 } 829 830 if (copy_from_user(&ex_hdr, buf + sizeof(hdr), sizeof(ex_hdr))) { 831 ret = -EFAULT; 832 goto out; 833 } 834 835 count -= sizeof(hdr) + sizeof(ex_hdr); 836 buf += sizeof(hdr) + sizeof(ex_hdr); 837 838 if ((hdr.in_words + ex_hdr.provider_in_words) * 8 != count) { 839 ret = -EINVAL; 840 goto out; 841 } 842 843 if (ex_hdr.cmd_hdr_reserved) { 844 ret = -EINVAL; 845 goto out; 846 } 847 848 if (ex_hdr.response) { 849 if (!hdr.out_words && !ex_hdr.provider_out_words) { 850 ret = -EINVAL; 851 goto out; 852 } 853 854 if (!access_ok(VERIFY_WRITE, 855 (void __user *) (unsigned long) ex_hdr.response, 856 (hdr.out_words + ex_hdr.provider_out_words) * 8)) { 857 ret = -EFAULT; 858 goto out; 859 } 860 } else { 861 if (hdr.out_words || ex_hdr.provider_out_words) { 862 ret = -EINVAL; 863 goto out; 864 } 865 } 866 867 INIT_UDATA_BUF_OR_NULL(&ucore, buf, (unsigned long) ex_hdr.response, 868 hdr.in_words * 8, hdr.out_words * 8); 869 870 INIT_UDATA_BUF_OR_NULL(&uhw, 871 buf + ucore.inlen, 872 (unsigned long) ex_hdr.response + ucore.outlen, 873 ex_hdr.provider_in_words * 8, 874 ex_hdr.provider_out_words * 8); 875 876 ret = uverbs_ex_cmd_table[command](file, 877 ib_dev, 878 &ucore, 879 &uhw); 880 if (!ret) 881 ret = written_count; 882 } else { 883 ret = -ENOSYS; 884 } 885 886out: 887 srcu_read_unlock(&file->device->disassociate_srcu, srcu_key); 888 return ret; 889} 890 891static int ib_uverbs_mmap(struct file *filp, struct vm_area_struct *vma) 892{ 893 struct ib_uverbs_file *file = filp->private_data; 894 struct ib_device *ib_dev; 895 int ret = 0; 896 int srcu_key; 897 898 srcu_key = srcu_read_lock(&file->device->disassociate_srcu); 899 ib_dev = srcu_dereference(file->device->ib_dev, 900 &file->device->disassociate_srcu); 901 if (!ib_dev) { 902 ret = -EIO; 903 goto out; 904 } 905 906 if (!file->ucontext) 907 ret = -ENODEV; 908 else 909 ret = ib_dev->mmap(file->ucontext, vma); 910out: 911 srcu_read_unlock(&file->device->disassociate_srcu, srcu_key); 912 return ret; 913} 914 915/* 916 * ib_uverbs_open() does not need the BKL: 917 * 918 * - the ib_uverbs_device structures are properly reference counted and 919 * everything else is purely local to the file being created, so 920 * races against other open calls are not a problem; 921 * - there is no ioctl method to race against; 922 * - the open method will either immediately run -ENXIO, or all 923 * required initialization will be done. 924 */ 925static int ib_uverbs_open(struct inode *inode, struct file *filp) 926{ 927 struct ib_uverbs_device *dev; 928 struct ib_uverbs_file *file; 929 struct ib_device *ib_dev; 930 int ret; 931 int module_dependent; 932 int srcu_key; 933 934 dev = container_of(inode->i_cdev->si_drv1, struct ib_uverbs_device, cdev); 935 if (!atomic_inc_not_zero(&dev->refcount)) 936 return -ENXIO; 937 938 srcu_key = srcu_read_lock(&dev->disassociate_srcu); 939 mutex_lock(&dev->lists_mutex); 940 ib_dev = srcu_dereference(dev->ib_dev, 941 &dev->disassociate_srcu); 942 if (!ib_dev) { 943 ret = -EIO; 944 goto err; 945 } 946 947 /* In case IB device supports disassociate ucontext, there is no hard 948 * dependency between uverbs device and its low level device. 949 */ 950 module_dependent = !(ib_dev->disassociate_ucontext); 951 952 if (module_dependent) { 953 if (!try_module_get(ib_dev->owner)) { 954 ret = -ENODEV; 955 goto err; 956 } 957 } 958 959 file = kzalloc(sizeof(*file), GFP_KERNEL); 960 if (!file) { 961 ret = -ENOMEM; 962 if (module_dependent) 963 goto err_module; 964 965 goto err; 966 } 967 968 file->device = dev; 969 file->ucontext = NULL; 970 file->async_file = NULL; 971 kref_init(&file->ref); 972 mutex_init(&file->mutex); 973 mutex_init(&file->cleanup_mutex); 974 975 filp->private_data = file; 976 kobject_get(&dev->kobj); 977 list_add_tail(&file->list, &dev->uverbs_file_list); 978 mutex_unlock(&dev->lists_mutex); 979 srcu_read_unlock(&dev->disassociate_srcu, srcu_key); 980 981 return nonseekable_open(inode, filp); 982 983err_module: 984 module_put(ib_dev->owner); 985 986err: 987 mutex_unlock(&dev->lists_mutex); 988 srcu_read_unlock(&dev->disassociate_srcu, srcu_key); 989 if (atomic_dec_and_test(&dev->refcount)) 990 ib_uverbs_comp_dev(dev); 991 992 return ret; 993} 994 995static int ib_uverbs_close(struct inode *inode, struct file *filp) 996{ 997 struct ib_uverbs_file *file = filp->private_data; 998 struct ib_uverbs_device *dev = file->device; 999 1000 mutex_lock(&file->cleanup_mutex); 1001 if (file->ucontext) { 1002 ib_uverbs_cleanup_ucontext(file, file->ucontext); 1003 file->ucontext = NULL; 1004 } 1005 mutex_unlock(&file->cleanup_mutex); 1006 1007 mutex_lock(&file->device->lists_mutex); 1008 if (!file->is_closed) { 1009 list_del(&file->list); 1010 file->is_closed = 1; 1011 } 1012 mutex_unlock(&file->device->lists_mutex); 1013 1014 if (file->async_file) 1015 kref_put(&file->async_file->ref, ib_uverbs_release_event_file); 1016 1017 kref_put(&file->ref, ib_uverbs_release_file); 1018 kobject_put(&dev->kobj); 1019 1020 return 0; 1021} 1022 1023static const struct file_operations uverbs_fops = { 1024 .owner = THIS_MODULE, 1025 .write = ib_uverbs_write, 1026 .open = ib_uverbs_open, 1027 .release = ib_uverbs_close, 1028 .llseek = no_llseek, 1029}; 1030 1031static const struct file_operations uverbs_mmap_fops = { 1032 .owner = THIS_MODULE, 1033 .write = ib_uverbs_write, 1034 .mmap = ib_uverbs_mmap, 1035 .open = ib_uverbs_open, 1036 .release = ib_uverbs_close, 1037 .llseek = no_llseek, 1038}; 1039 1040static struct ib_client uverbs_client = { 1041 .name = "uverbs", 1042 .add = ib_uverbs_add_one, 1043 .remove = ib_uverbs_remove_one 1044}; 1045 1046static ssize_t show_ibdev(struct device *device, struct device_attribute *attr, 1047 char *buf) 1048{ 1049 int ret = -ENODEV; 1050 int srcu_key; 1051 struct ib_uverbs_device *dev = dev_get_drvdata(device); 1052 struct ib_device *ib_dev; 1053 1054 if (!dev) 1055 return -ENODEV; 1056 1057 srcu_key = srcu_read_lock(&dev->disassociate_srcu); 1058 ib_dev = srcu_dereference(dev->ib_dev, &dev->disassociate_srcu); 1059 if (ib_dev) 1060 ret = sprintf(buf, "%s\n", ib_dev->name); 1061 srcu_read_unlock(&dev->disassociate_srcu, srcu_key); 1062 1063 return ret; 1064} 1065static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL); 1066 1067static ssize_t show_dev_abi_version(struct device *device, 1068 struct device_attribute *attr, char *buf) 1069{ 1070 struct ib_uverbs_device *dev = dev_get_drvdata(device); 1071 int ret = -ENODEV; 1072 int srcu_key; 1073 struct ib_device *ib_dev; 1074 1075 if (!dev) 1076 return -ENODEV; 1077 srcu_key = srcu_read_lock(&dev->disassociate_srcu); 1078 ib_dev = srcu_dereference(dev->ib_dev, &dev->disassociate_srcu); 1079 if (ib_dev) 1080 ret = sprintf(buf, "%d\n", ib_dev->uverbs_abi_ver); 1081 srcu_read_unlock(&dev->disassociate_srcu, srcu_key); 1082 1083 return ret; 1084} 1085static DEVICE_ATTR(abi_version, S_IRUGO, show_dev_abi_version, NULL); 1086 1087static CLASS_ATTR_STRING(abi_version, S_IRUGO, 1088 __stringify(IB_USER_VERBS_ABI_VERSION)); 1089 1090static dev_t overflow_maj; 1091static DECLARE_BITMAP(overflow_map, IB_UVERBS_MAX_DEVICES); 1092 1093/* 1094 * If we have more than IB_UVERBS_MAX_DEVICES, dynamically overflow by 1095 * requesting a new major number and doubling the number of max devices we 1096 * support. It's stupid, but simple. 1097 */ 1098static int find_overflow_devnum(void) 1099{ 1100 int ret; 1101 1102 if (!overflow_maj) { 1103 ret = alloc_chrdev_region(&overflow_maj, 0, IB_UVERBS_MAX_DEVICES, 1104 "infiniband_verbs"); 1105 if (ret) { 1106 pr_err("user_verbs: couldn't register dynamic device number\n"); 1107 return ret; 1108 } 1109 } 1110 1111 ret = find_first_zero_bit(overflow_map, IB_UVERBS_MAX_DEVICES); 1112 if (ret >= IB_UVERBS_MAX_DEVICES) 1113 return -1; 1114 1115 return ret; 1116} 1117 1118static ssize_t 1119show_dev_device(struct device *device, struct device_attribute *attr, char *buf) 1120{ 1121 struct ib_uverbs_device *dev = dev_get_drvdata(device); 1122 1123 if (!dev || !dev->ib_dev->dma_device) 1124 return -ENODEV; 1125 1126 return sprintf(buf, "0x%04x\n", 1127 ((struct pci_dev *)dev->ib_dev->dma_device)->device); 1128} 1129static DEVICE_ATTR(device, S_IRUGO, show_dev_device, NULL); 1130 1131static ssize_t 1132show_dev_vendor(struct device *device, struct device_attribute *attr, char *buf) 1133{ 1134 struct ib_uverbs_device *dev = dev_get_drvdata(device); 1135 1136 if (!dev || !dev->ib_dev->dma_device) 1137 return -ENODEV; 1138 1139 return sprintf(buf, "0x%04x\n", 1140 ((struct pci_dev *)dev->ib_dev->dma_device)->vendor); 1141} 1142static DEVICE_ATTR(vendor, S_IRUGO, show_dev_vendor, NULL); 1143 1144struct attribute *device_attrs[] = 1145{ 1146 &dev_attr_device.attr, 1147 &dev_attr_vendor.attr, 1148 NULL 1149}; 1150 1151static struct attribute_group device_group = { 1152 .name = "device", 1153 .attrs = device_attrs 1154}; 1155 1156static void ib_uverbs_add_one(struct ib_device *device) 1157{ 1158 int devnum; 1159 dev_t base; 1160 struct ib_uverbs_device *uverbs_dev; 1161 int ret; 1162 1163 if (!device->alloc_ucontext) 1164 return; 1165 1166 uverbs_dev = kzalloc(sizeof *uverbs_dev, GFP_KERNEL); 1167 if (!uverbs_dev) 1168 return; 1169 1170 ret = init_srcu_struct(&uverbs_dev->disassociate_srcu); 1171 if (ret) { 1172 kfree(uverbs_dev); 1173 return; 1174 } 1175 1176 atomic_set(&uverbs_dev->refcount, 1); 1177 init_completion(&uverbs_dev->comp); 1178 uverbs_dev->xrcd_tree = RB_ROOT; 1179 mutex_init(&uverbs_dev->xrcd_tree_mutex); 1180 kobject_init(&uverbs_dev->kobj, &ib_uverbs_dev_ktype); 1181 mutex_init(&uverbs_dev->lists_mutex); 1182 INIT_LIST_HEAD(&uverbs_dev->uverbs_file_list); 1183 INIT_LIST_HEAD(&uverbs_dev->uverbs_events_file_list); 1184 1185 spin_lock(&map_lock); 1186 devnum = find_first_zero_bit(dev_map, IB_UVERBS_MAX_DEVICES); 1187 if (devnum >= IB_UVERBS_MAX_DEVICES) { 1188 spin_unlock(&map_lock); 1189 devnum = find_overflow_devnum(); 1190 if (devnum < 0) 1191 goto err; 1192 1193 spin_lock(&map_lock); 1194 uverbs_dev->devnum = devnum + IB_UVERBS_MAX_DEVICES; 1195 base = devnum + overflow_maj; 1196 set_bit(devnum, overflow_map); 1197 } else { 1198 uverbs_dev->devnum = devnum; 1199 base = devnum + IB_UVERBS_BASE_DEV; 1200 set_bit(devnum, dev_map); 1201 } 1202 spin_unlock(&map_lock); 1203 1204 rcu_assign_pointer(uverbs_dev->ib_dev, device); 1205 uverbs_dev->num_comp_vectors = device->num_comp_vectors; 1206 1207 cdev_init(&uverbs_dev->cdev, NULL); 1208 uverbs_dev->cdev.owner = THIS_MODULE; 1209 uverbs_dev->cdev.ops = device->mmap ? &uverbs_mmap_fops : &uverbs_fops; 1210 uverbs_dev->cdev.kobj.parent = &uverbs_dev->kobj; 1211 kobject_set_name(&uverbs_dev->cdev.kobj, "uverbs%d", uverbs_dev->devnum); 1212 if (cdev_add(&uverbs_dev->cdev, base, 1)) 1213 goto err_cdev; 1214 1215 uverbs_dev->dev = device_create(uverbs_class, device->dma_device, 1216 uverbs_dev->cdev.dev, uverbs_dev, 1217 "uverbs%d", uverbs_dev->devnum); 1218 if (IS_ERR(uverbs_dev->dev)) 1219 goto err_cdev; 1220 1221 if (device_create_file(uverbs_dev->dev, &dev_attr_ibdev)) 1222 goto err_class; 1223 if (device_create_file(uverbs_dev->dev, &dev_attr_abi_version)) 1224 goto err_class; 1225 if (sysfs_create_group(&uverbs_dev->dev->kobj, &device_group)) 1226 goto err_class; 1227 1228 ib_set_client_data(device, &uverbs_client, uverbs_dev); 1229 1230 return; 1231 1232err_class: 1233 device_destroy(uverbs_class, uverbs_dev->cdev.dev); 1234 1235err_cdev: 1236 cdev_del(&uverbs_dev->cdev); 1237 if (uverbs_dev->devnum < IB_UVERBS_MAX_DEVICES) 1238 clear_bit(devnum, dev_map); 1239 else 1240 clear_bit(devnum, overflow_map); 1241 1242err: 1243 if (atomic_dec_and_test(&uverbs_dev->refcount)) 1244 ib_uverbs_comp_dev(uverbs_dev); 1245 wait_for_completion(&uverbs_dev->comp); 1246 kobject_put(&uverbs_dev->kobj); 1247 return; 1248} 1249 1250static void ib_uverbs_free_hw_resources(struct ib_uverbs_device *uverbs_dev, 1251 struct ib_device *ib_dev) 1252{ 1253 struct ib_uverbs_file *file; 1254 struct ib_uverbs_event_file *event_file; 1255 struct ib_event event; 1256 1257 /* Pending running commands to terminate */ 1258 synchronize_srcu(&uverbs_dev->disassociate_srcu); 1259 event.event = IB_EVENT_DEVICE_FATAL; 1260 event.element.port_num = 0; 1261 event.device = ib_dev; 1262 1263 mutex_lock(&uverbs_dev->lists_mutex); 1264 while (!list_empty(&uverbs_dev->uverbs_file_list)) { 1265 struct ib_ucontext *ucontext; 1266 file = list_first_entry(&uverbs_dev->uverbs_file_list, 1267 struct ib_uverbs_file, list); 1268 file->is_closed = 1; 1269 list_del(&file->list); 1270 kref_get(&file->ref); 1271 mutex_unlock(&uverbs_dev->lists_mutex); 1272 1273 ib_uverbs_event_handler(&file->event_handler, &event); 1274 1275 mutex_lock(&file->cleanup_mutex); 1276 ucontext = file->ucontext; 1277 file->ucontext = NULL; 1278 mutex_unlock(&file->cleanup_mutex); 1279 1280 /* At this point ib_uverbs_close cannot be running 1281 * ib_uverbs_cleanup_ucontext 1282 */ 1283 if (ucontext) { 1284 /* We must release the mutex before going ahead and 1285 * calling disassociate_ucontext. disassociate_ucontext 1286 * might end up indirectly calling uverbs_close, 1287 * for example due to freeing the resources 1288 * (e.g mmput). 1289 */ 1290 ib_dev->disassociate_ucontext(ucontext); 1291 ib_uverbs_cleanup_ucontext(file, ucontext); 1292 } 1293 1294 mutex_lock(&uverbs_dev->lists_mutex); 1295 kref_put(&file->ref, ib_uverbs_release_file); 1296 } 1297 1298 while (!list_empty(&uverbs_dev->uverbs_events_file_list)) { 1299 event_file = list_first_entry(&uverbs_dev-> 1300 uverbs_events_file_list, 1301 struct ib_uverbs_event_file, 1302 list); 1303 spin_lock_irq(&event_file->lock); 1304 event_file->is_closed = 1; 1305 spin_unlock_irq(&event_file->lock); 1306 1307 list_del(&event_file->list); 1308 if (event_file->is_async) { 1309 ib_unregister_event_handler(&event_file->uverbs_file-> 1310 event_handler); 1311 event_file->uverbs_file->event_handler.device = NULL; 1312 } 1313 1314 wake_up_interruptible(&event_file->poll_wait); 1315 kill_fasync(&event_file->async_queue, SIGIO, POLL_IN); 1316 } 1317 mutex_unlock(&uverbs_dev->lists_mutex); 1318} 1319 1320static void ib_uverbs_remove_one(struct ib_device *device, void *client_data) 1321{ 1322 struct ib_uverbs_device *uverbs_dev = client_data; 1323 int wait_clients = 1; 1324 1325 if (!uverbs_dev) 1326 return; 1327 1328 sysfs_remove_group(&uverbs_dev->dev->kobj, &device_group); 1329 dev_set_drvdata(uverbs_dev->dev, NULL); 1330 device_destroy(uverbs_class, uverbs_dev->cdev.dev); 1331 cdev_del(&uverbs_dev->cdev); 1332 1333 if (uverbs_dev->devnum < IB_UVERBS_MAX_DEVICES) 1334 clear_bit(uverbs_dev->devnum, dev_map); 1335 else 1336 clear_bit(uverbs_dev->devnum - IB_UVERBS_MAX_DEVICES, overflow_map); 1337 1338 if (device->disassociate_ucontext) { 1339 /* We disassociate HW resources and immediately return. 1340 * Userspace will see a EIO errno for all future access. 1341 * Upon returning, ib_device may be freed internally and is not 1342 * valid any more. 1343 * uverbs_device is still available until all clients close 1344 * their files, then the uverbs device ref count will be zero 1345 * and its resources will be freed. 1346 * Note: At this point no more files can be opened since the 1347 * cdev was deleted, however active clients can still issue 1348 * commands and close their open files. 1349 */ 1350 rcu_assign_pointer(uverbs_dev->ib_dev, NULL); 1351 ib_uverbs_free_hw_resources(uverbs_dev, device); 1352 wait_clients = 0; 1353 } 1354 1355 if (atomic_dec_and_test(&uverbs_dev->refcount)) 1356 ib_uverbs_comp_dev(uverbs_dev); 1357 if (wait_clients) 1358 wait_for_completion(&uverbs_dev->comp); 1359 kobject_put(&uverbs_dev->kobj); 1360} 1361 1362static char *uverbs_devnode(struct device *dev, umode_t *mode) 1363{ 1364 if (mode) 1365 *mode = 0666; 1366 return kasprintf(GFP_KERNEL, "infiniband/%s", dev_name(dev)); 1367} 1368 1369static int __init ib_uverbs_init(void) 1370{ 1371 int ret; 1372 1373 ret = register_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES, 1374 "infiniband_verbs"); 1375 if (ret) { 1376 pr_err("user_verbs: couldn't register device number\n"); 1377 goto out; 1378 } 1379 1380 uverbs_class = class_create(THIS_MODULE, "infiniband_verbs"); 1381 if (IS_ERR(uverbs_class)) { 1382 ret = PTR_ERR(uverbs_class); 1383 pr_err("user_verbs: couldn't create class infiniband_verbs\n"); 1384 goto out_chrdev; 1385 } 1386 1387 uverbs_class->devnode = uverbs_devnode; 1388 1389 ret = class_create_file(uverbs_class, &class_attr_abi_version.attr); 1390 if (ret) { 1391 pr_err("user_verbs: couldn't create abi_version attribute\n"); 1392 goto out_class; 1393 } 1394 1395 ret = ib_register_client(&uverbs_client); 1396 if (ret) { 1397 pr_err("user_verbs: couldn't register client\n"); 1398 goto out_class; 1399 } 1400 1401 return 0; 1402 1403out_class: 1404 class_destroy(uverbs_class); 1405 1406out_chrdev: 1407 unregister_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES); 1408 1409out: 1410 return ret; 1411} 1412 1413static void __exit ib_uverbs_cleanup(void) 1414{ 1415 ib_unregister_client(&uverbs_client); 1416 class_destroy(uverbs_class); 1417 unregister_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES); 1418 if (overflow_maj) 1419 unregister_chrdev_region(overflow_maj, IB_UVERBS_MAX_DEVICES); 1420 idr_destroy(&ib_uverbs_pd_idr); 1421 idr_destroy(&ib_uverbs_mr_idr); 1422 idr_destroy(&ib_uverbs_mw_idr); 1423 idr_destroy(&ib_uverbs_ah_idr); 1424 idr_destroy(&ib_uverbs_cq_idr); 1425 idr_destroy(&ib_uverbs_qp_idr); 1426 idr_destroy(&ib_uverbs_srq_idr); 1427} 1428 1429module_init_order(ib_uverbs_init, SI_ORDER_THIRD); 1430module_exit(ib_uverbs_cleanup); 1431