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