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