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 (!file->ucontext && 740 (command != IB_USER_VERBS_CMD_GET_CONTEXT || extended)) 741 return -EINVAL; 742 743 if (extended) { 744 if (count < (sizeof(hdr) + sizeof(ex_hdr))) 745 return -EINVAL; 746 if (copy_from_user(&ex_hdr, buf + sizeof(hdr), sizeof(ex_hdr))) 747 return -EFAULT; 748 } 749 750 ret = verify_hdr(&hdr, &ex_hdr, count, extended); 751 if (ret) 752 return ret; 753 754 srcu_key = srcu_read_lock(&file->device->disassociate_srcu); 755 ib_dev = srcu_dereference(file->device->ib_dev, 756 &file->device->disassociate_srcu); 757 if (!ib_dev) { 758 ret = -EIO; 759 goto out; 760 } 761 762 if (!verify_command_mask(ib_dev, command, extended)) { 763 ret = -EOPNOTSUPP; 764 goto out; 765 } 766 767 buf += sizeof(hdr); 768 769 if (!extended) { 770 ret = uverbs_cmd_table[command](file, ib_dev, buf, 771 hdr.in_words * 4, 772 hdr.out_words * 4); 773 } else { 774 struct ib_udata ucore; 775 struct ib_udata uhw; 776 777 buf += sizeof(ex_hdr); 778 779 ib_uverbs_init_udata_buf_or_null(&ucore, buf, 780 u64_to_user_ptr(ex_hdr.response), 781 hdr.in_words * 8, hdr.out_words * 8); 782 783 ib_uverbs_init_udata_buf_or_null(&uhw, 784 buf + ucore.inlen, 785 u64_to_user_ptr(ex_hdr.response) + ucore.outlen, 786 ex_hdr.provider_in_words * 8, 787 ex_hdr.provider_out_words * 8); 788 789 ret = uverbs_ex_cmd_table[command](file, ib_dev, &ucore, &uhw); 790 ret = (ret) ? : count; 791 } 792 793 out: 794 srcu_read_unlock(&file->device->disassociate_srcu, srcu_key); 795 return ret; 796 } 797 798 static int ib_uverbs_mmap(struct file *filp, struct vm_area_struct *vma) 799 { 800 struct ib_uverbs_file *file = filp->private_data; 801 struct ib_device *ib_dev; 802 int ret = 0; 803 int srcu_key; 804 805 srcu_key = srcu_read_lock(&file->device->disassociate_srcu); 806 ib_dev = srcu_dereference(file->device->ib_dev, 807 &file->device->disassociate_srcu); 808 if (!ib_dev) { 809 ret = -EIO; 810 goto out; 811 } 812 813 if (!file->ucontext) 814 ret = -ENODEV; 815 else 816 ret = ib_dev->mmap(file->ucontext, vma); 817 out: 818 srcu_read_unlock(&file->device->disassociate_srcu, srcu_key); 819 return ret; 820 } 821 822 /* 823 * ib_uverbs_open() does not need the BKL: 824 * 825 * - the ib_uverbs_device structures are properly reference counted and 826 * everything else is purely local to the file being created, so 827 * races against other open calls are not a problem; 828 * - there is no ioctl method to race against; 829 * - the open method will either immediately run -ENXIO, or all 830 * required initialization will be done. 831 */ 832 static int ib_uverbs_open(struct inode *inode, struct file *filp) 833 { 834 struct ib_uverbs_device *dev; 835 struct ib_uverbs_file *file; 836 struct ib_device *ib_dev; 837 int ret; 838 int module_dependent; 839 int srcu_key; 840 841 dev = container_of(inode->i_cdev, struct ib_uverbs_device, cdev); 842 if (!atomic_inc_not_zero(&dev->refcount)) 843 return -ENXIO; 844 845 srcu_key = srcu_read_lock(&dev->disassociate_srcu); 846 mutex_lock(&dev->lists_mutex); 847 ib_dev = srcu_dereference(dev->ib_dev, 848 &dev->disassociate_srcu); 849 if (!ib_dev) { 850 ret = -EIO; 851 goto err; 852 } 853 854 /* In case IB device supports disassociate ucontext, there is no hard 855 * dependency between uverbs device and its low level device. 856 */ 857 module_dependent = !(ib_dev->disassociate_ucontext); 858 859 if (module_dependent) { 860 if (!try_module_get(ib_dev->owner)) { 861 ret = -ENODEV; 862 goto err; 863 } 864 } 865 866 file = kzalloc(sizeof(*file), GFP_KERNEL); 867 if (!file) { 868 ret = -ENOMEM; 869 if (module_dependent) 870 goto err_module; 871 872 goto err; 873 } 874 875 file->device = dev; 876 spin_lock_init(&file->idr_lock); 877 idr_init(&file->idr); 878 file->ucontext = NULL; 879 file->async_file = NULL; 880 kref_init(&file->ref); 881 mutex_init(&file->mutex); 882 mutex_init(&file->cleanup_mutex); 883 884 filp->private_data = file; 885 kobject_get(&dev->kobj); 886 list_add_tail(&file->list, &dev->uverbs_file_list); 887 mutex_unlock(&dev->lists_mutex); 888 srcu_read_unlock(&dev->disassociate_srcu, srcu_key); 889 890 return nonseekable_open(inode, filp); 891 892 err_module: 893 module_put(ib_dev->owner); 894 895 err: 896 mutex_unlock(&dev->lists_mutex); 897 srcu_read_unlock(&dev->disassociate_srcu, srcu_key); 898 if (atomic_dec_and_test(&dev->refcount)) 899 ib_uverbs_comp_dev(dev); 900 901 return ret; 902 } 903 904 static int ib_uverbs_close(struct inode *inode, struct file *filp) 905 { 906 struct ib_uverbs_file *file = filp->private_data; 907 908 mutex_lock(&file->cleanup_mutex); 909 if (file->ucontext) { 910 ib_uverbs_cleanup_ucontext(file, file->ucontext, false); 911 file->ucontext = NULL; 912 } 913 mutex_unlock(&file->cleanup_mutex); 914 idr_destroy(&file->idr); 915 916 mutex_lock(&file->device->lists_mutex); 917 if (!file->is_closed) { 918 list_del(&file->list); 919 file->is_closed = 1; 920 } 921 mutex_unlock(&file->device->lists_mutex); 922 923 if (file->async_file) 924 kref_put(&file->async_file->ref, 925 ib_uverbs_release_async_event_file); 926 927 kref_put(&file->ref, ib_uverbs_release_file); 928 929 return 0; 930 } 931 932 static const struct file_operations uverbs_fops = { 933 .owner = THIS_MODULE, 934 .write = ib_uverbs_write, 935 .open = ib_uverbs_open, 936 .release = ib_uverbs_close, 937 .llseek = no_llseek, 938 .unlocked_ioctl = ib_uverbs_ioctl, 939 .compat_ioctl = ib_uverbs_ioctl, 940 }; 941 942 static const struct file_operations uverbs_mmap_fops = { 943 .owner = THIS_MODULE, 944 .write = ib_uverbs_write, 945 .mmap = ib_uverbs_mmap, 946 .open = ib_uverbs_open, 947 .release = ib_uverbs_close, 948 .llseek = no_llseek, 949 .unlocked_ioctl = ib_uverbs_ioctl, 950 .compat_ioctl = ib_uverbs_ioctl, 951 }; 952 953 static struct ib_client uverbs_client = { 954 .name = "uverbs", 955 .add = ib_uverbs_add_one, 956 .remove = ib_uverbs_remove_one 957 }; 958 959 static ssize_t show_ibdev(struct device *device, struct device_attribute *attr, 960 char *buf) 961 { 962 int ret = -ENODEV; 963 int srcu_key; 964 struct ib_uverbs_device *dev = dev_get_drvdata(device); 965 struct ib_device *ib_dev; 966 967 if (!dev) 968 return -ENODEV; 969 970 srcu_key = srcu_read_lock(&dev->disassociate_srcu); 971 ib_dev = srcu_dereference(dev->ib_dev, &dev->disassociate_srcu); 972 if (ib_dev) 973 ret = sprintf(buf, "%s\n", ib_dev->name); 974 srcu_read_unlock(&dev->disassociate_srcu, srcu_key); 975 976 return ret; 977 } 978 static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL); 979 980 static ssize_t show_dev_abi_version(struct device *device, 981 struct device_attribute *attr, char *buf) 982 { 983 struct ib_uverbs_device *dev = dev_get_drvdata(device); 984 int ret = -ENODEV; 985 int srcu_key; 986 struct ib_device *ib_dev; 987 988 if (!dev) 989 return -ENODEV; 990 srcu_key = srcu_read_lock(&dev->disassociate_srcu); 991 ib_dev = srcu_dereference(dev->ib_dev, &dev->disassociate_srcu); 992 if (ib_dev) 993 ret = sprintf(buf, "%d\n", ib_dev->uverbs_abi_ver); 994 srcu_read_unlock(&dev->disassociate_srcu, srcu_key); 995 996 return ret; 997 } 998 static DEVICE_ATTR(abi_version, S_IRUGO, show_dev_abi_version, NULL); 999 1000 static CLASS_ATTR_STRING(abi_version, S_IRUGO, 1001 __stringify(IB_USER_VERBS_ABI_VERSION)); 1002 1003 static void ib_uverbs_add_one(struct ib_device *device) 1004 { 1005 int devnum; 1006 dev_t base; 1007 struct ib_uverbs_device *uverbs_dev; 1008 int ret; 1009 1010 if (!device->alloc_ucontext) 1011 return; 1012 1013 uverbs_dev = kzalloc(sizeof(*uverbs_dev), GFP_KERNEL); 1014 if (!uverbs_dev) 1015 return; 1016 1017 ret = init_srcu_struct(&uverbs_dev->disassociate_srcu); 1018 if (ret) { 1019 kfree(uverbs_dev); 1020 return; 1021 } 1022 1023 atomic_set(&uverbs_dev->refcount, 1); 1024 init_completion(&uverbs_dev->comp); 1025 uverbs_dev->xrcd_tree = RB_ROOT; 1026 mutex_init(&uverbs_dev->xrcd_tree_mutex); 1027 kobject_init(&uverbs_dev->kobj, &ib_uverbs_dev_ktype); 1028 mutex_init(&uverbs_dev->lists_mutex); 1029 INIT_LIST_HEAD(&uverbs_dev->uverbs_file_list); 1030 INIT_LIST_HEAD(&uverbs_dev->uverbs_events_file_list); 1031 1032 devnum = find_first_zero_bit(dev_map, IB_UVERBS_MAX_DEVICES); 1033 if (devnum >= IB_UVERBS_MAX_DEVICES) 1034 goto err; 1035 uverbs_dev->devnum = devnum; 1036 set_bit(devnum, dev_map); 1037 if (devnum >= IB_UVERBS_NUM_FIXED_MINOR) 1038 base = dynamic_uverbs_dev + devnum - IB_UVERBS_NUM_FIXED_MINOR; 1039 else 1040 base = IB_UVERBS_BASE_DEV + devnum; 1041 1042 rcu_assign_pointer(uverbs_dev->ib_dev, device); 1043 uverbs_dev->num_comp_vectors = device->num_comp_vectors; 1044 1045 cdev_init(&uverbs_dev->cdev, NULL); 1046 uverbs_dev->cdev.owner = THIS_MODULE; 1047 uverbs_dev->cdev.ops = device->mmap ? &uverbs_mmap_fops : &uverbs_fops; 1048 cdev_set_parent(&uverbs_dev->cdev, &uverbs_dev->kobj); 1049 kobject_set_name(&uverbs_dev->cdev.kobj, "uverbs%d", uverbs_dev->devnum); 1050 if (cdev_add(&uverbs_dev->cdev, base, 1)) 1051 goto err_cdev; 1052 1053 uverbs_dev->dev = device_create(uverbs_class, device->dev.parent, 1054 uverbs_dev->cdev.dev, uverbs_dev, 1055 "uverbs%d", uverbs_dev->devnum); 1056 if (IS_ERR(uverbs_dev->dev)) 1057 goto err_cdev; 1058 1059 if (device_create_file(uverbs_dev->dev, &dev_attr_ibdev)) 1060 goto err_class; 1061 if (device_create_file(uverbs_dev->dev, &dev_attr_abi_version)) 1062 goto err_class; 1063 1064 if (!device->specs_root) { 1065 const struct uverbs_object_tree_def *default_root[] = { 1066 uverbs_default_get_objects()}; 1067 1068 uverbs_dev->specs_root = uverbs_alloc_spec_tree(1, 1069 default_root); 1070 if (IS_ERR(uverbs_dev->specs_root)) 1071 goto err_class; 1072 1073 device->specs_root = uverbs_dev->specs_root; 1074 } 1075 1076 ib_set_client_data(device, &uverbs_client, uverbs_dev); 1077 1078 return; 1079 1080 err_class: 1081 device_destroy(uverbs_class, uverbs_dev->cdev.dev); 1082 1083 err_cdev: 1084 cdev_del(&uverbs_dev->cdev); 1085 clear_bit(devnum, dev_map); 1086 1087 err: 1088 if (atomic_dec_and_test(&uverbs_dev->refcount)) 1089 ib_uverbs_comp_dev(uverbs_dev); 1090 wait_for_completion(&uverbs_dev->comp); 1091 kobject_put(&uverbs_dev->kobj); 1092 return; 1093 } 1094 1095 static void ib_uverbs_disassociate_ucontext(struct ib_ucontext *ibcontext) 1096 { 1097 struct ib_device *ib_dev = ibcontext->device; 1098 struct task_struct *owning_process = NULL; 1099 struct mm_struct *owning_mm = NULL; 1100 1101 owning_process = get_pid_task(ibcontext->tgid, PIDTYPE_PID); 1102 if (!owning_process) 1103 return; 1104 1105 owning_mm = get_task_mm(owning_process); 1106 if (!owning_mm) { 1107 pr_info("no mm, disassociate ucontext is pending task termination\n"); 1108 while (1) { 1109 put_task_struct(owning_process); 1110 usleep_range(1000, 2000); 1111 owning_process = get_pid_task(ibcontext->tgid, 1112 PIDTYPE_PID); 1113 if (!owning_process || 1114 owning_process->state == TASK_DEAD) { 1115 pr_info("disassociate ucontext done, task was terminated\n"); 1116 /* in case task was dead need to release the 1117 * task struct. 1118 */ 1119 if (owning_process) 1120 put_task_struct(owning_process); 1121 return; 1122 } 1123 } 1124 } 1125 1126 down_write(&owning_mm->mmap_sem); 1127 ib_dev->disassociate_ucontext(ibcontext); 1128 up_write(&owning_mm->mmap_sem); 1129 mmput(owning_mm); 1130 put_task_struct(owning_process); 1131 } 1132 1133 static void ib_uverbs_free_hw_resources(struct ib_uverbs_device *uverbs_dev, 1134 struct ib_device *ib_dev) 1135 { 1136 struct ib_uverbs_file *file; 1137 struct ib_uverbs_async_event_file *event_file; 1138 struct ib_event event; 1139 1140 /* Pending running commands to terminate */ 1141 synchronize_srcu(&uverbs_dev->disassociate_srcu); 1142 event.event = IB_EVENT_DEVICE_FATAL; 1143 event.element.port_num = 0; 1144 event.device = ib_dev; 1145 1146 mutex_lock(&uverbs_dev->lists_mutex); 1147 while (!list_empty(&uverbs_dev->uverbs_file_list)) { 1148 struct ib_ucontext *ucontext; 1149 file = list_first_entry(&uverbs_dev->uverbs_file_list, 1150 struct ib_uverbs_file, list); 1151 file->is_closed = 1; 1152 list_del(&file->list); 1153 kref_get(&file->ref); 1154 mutex_unlock(&uverbs_dev->lists_mutex); 1155 1156 1157 mutex_lock(&file->cleanup_mutex); 1158 ucontext = file->ucontext; 1159 file->ucontext = NULL; 1160 mutex_unlock(&file->cleanup_mutex); 1161 1162 /* At this point ib_uverbs_close cannot be running 1163 * ib_uverbs_cleanup_ucontext 1164 */ 1165 if (ucontext) { 1166 /* We must release the mutex before going ahead and 1167 * calling disassociate_ucontext. disassociate_ucontext 1168 * might end up indirectly calling uverbs_close, 1169 * for example due to freeing the resources 1170 * (e.g mmput). 1171 */ 1172 ib_uverbs_event_handler(&file->event_handler, &event); 1173 ib_uverbs_disassociate_ucontext(ucontext); 1174 mutex_lock(&file->cleanup_mutex); 1175 ib_uverbs_cleanup_ucontext(file, ucontext, true); 1176 mutex_unlock(&file->cleanup_mutex); 1177 } 1178 1179 mutex_lock(&uverbs_dev->lists_mutex); 1180 kref_put(&file->ref, ib_uverbs_release_file); 1181 } 1182 1183 while (!list_empty(&uverbs_dev->uverbs_events_file_list)) { 1184 event_file = list_first_entry(&uverbs_dev-> 1185 uverbs_events_file_list, 1186 struct ib_uverbs_async_event_file, 1187 list); 1188 spin_lock_irq(&event_file->ev_queue.lock); 1189 event_file->ev_queue.is_closed = 1; 1190 spin_unlock_irq(&event_file->ev_queue.lock); 1191 1192 list_del(&event_file->list); 1193 ib_unregister_event_handler( 1194 &event_file->uverbs_file->event_handler); 1195 event_file->uverbs_file->event_handler.device = 1196 NULL; 1197 1198 wake_up_interruptible(&event_file->ev_queue.poll_wait); 1199 kill_fasync(&event_file->ev_queue.async_queue, SIGIO, POLL_IN); 1200 } 1201 mutex_unlock(&uverbs_dev->lists_mutex); 1202 } 1203 1204 static void ib_uverbs_remove_one(struct ib_device *device, void *client_data) 1205 { 1206 struct ib_uverbs_device *uverbs_dev = client_data; 1207 int wait_clients = 1; 1208 1209 if (!uverbs_dev) 1210 return; 1211 1212 dev_set_drvdata(uverbs_dev->dev, NULL); 1213 device_destroy(uverbs_class, uverbs_dev->cdev.dev); 1214 cdev_del(&uverbs_dev->cdev); 1215 clear_bit(uverbs_dev->devnum, dev_map); 1216 1217 if (device->disassociate_ucontext) { 1218 /* We disassociate HW resources and immediately return. 1219 * Userspace will see a EIO errno for all future access. 1220 * Upon returning, ib_device may be freed internally and is not 1221 * valid any more. 1222 * uverbs_device is still available until all clients close 1223 * their files, then the uverbs device ref count will be zero 1224 * and its resources will be freed. 1225 * Note: At this point no more files can be opened since the 1226 * cdev was deleted, however active clients can still issue 1227 * commands and close their open files. 1228 */ 1229 rcu_assign_pointer(uverbs_dev->ib_dev, NULL); 1230 ib_uverbs_free_hw_resources(uverbs_dev, device); 1231 wait_clients = 0; 1232 } 1233 1234 if (atomic_dec_and_test(&uverbs_dev->refcount)) 1235 ib_uverbs_comp_dev(uverbs_dev); 1236 if (wait_clients) 1237 wait_for_completion(&uverbs_dev->comp); 1238 if (uverbs_dev->specs_root) { 1239 uverbs_free_spec_tree(uverbs_dev->specs_root); 1240 device->specs_root = NULL; 1241 } 1242 1243 kobject_put(&uverbs_dev->kobj); 1244 } 1245 1246 static char *uverbs_devnode(struct device *dev, umode_t *mode) 1247 { 1248 if (mode) 1249 *mode = 0666; 1250 return kasprintf(GFP_KERNEL, "infiniband/%s", dev_name(dev)); 1251 } 1252 1253 static int __init ib_uverbs_init(void) 1254 { 1255 int ret; 1256 1257 ret = register_chrdev_region(IB_UVERBS_BASE_DEV, 1258 IB_UVERBS_NUM_FIXED_MINOR, 1259 "infiniband_verbs"); 1260 if (ret) { 1261 pr_err("user_verbs: couldn't register device number\n"); 1262 goto out; 1263 } 1264 1265 ret = alloc_chrdev_region(&dynamic_uverbs_dev, 0, 1266 IB_UVERBS_NUM_DYNAMIC_MINOR, 1267 "infiniband_verbs"); 1268 if (ret) { 1269 pr_err("couldn't register dynamic device number\n"); 1270 goto out_alloc; 1271 } 1272 1273 uverbs_class = class_create(THIS_MODULE, "infiniband_verbs"); 1274 if (IS_ERR(uverbs_class)) { 1275 ret = PTR_ERR(uverbs_class); 1276 pr_err("user_verbs: couldn't create class infiniband_verbs\n"); 1277 goto out_chrdev; 1278 } 1279 1280 uverbs_class->devnode = uverbs_devnode; 1281 1282 ret = class_create_file(uverbs_class, &class_attr_abi_version.attr); 1283 if (ret) { 1284 pr_err("user_verbs: couldn't create abi_version attribute\n"); 1285 goto out_class; 1286 } 1287 1288 ret = ib_register_client(&uverbs_client); 1289 if (ret) { 1290 pr_err("user_verbs: couldn't register client\n"); 1291 goto out_class; 1292 } 1293 1294 return 0; 1295 1296 out_class: 1297 class_destroy(uverbs_class); 1298 1299 out_chrdev: 1300 unregister_chrdev_region(dynamic_uverbs_dev, 1301 IB_UVERBS_NUM_DYNAMIC_MINOR); 1302 1303 out_alloc: 1304 unregister_chrdev_region(IB_UVERBS_BASE_DEV, 1305 IB_UVERBS_NUM_FIXED_MINOR); 1306 1307 out: 1308 return ret; 1309 } 1310 1311 static void __exit ib_uverbs_cleanup(void) 1312 { 1313 ib_unregister_client(&uverbs_client); 1314 class_destroy(uverbs_class); 1315 unregister_chrdev_region(IB_UVERBS_BASE_DEV, 1316 IB_UVERBS_NUM_FIXED_MINOR); 1317 unregister_chrdev_region(dynamic_uverbs_dev, 1318 IB_UVERBS_NUM_DYNAMIC_MINOR); 1319 } 1320 1321 module_init(ib_uverbs_init); 1322 module_exit(ib_uverbs_cleanup); 1323