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 #include <linux/sched/mm.h>
49
50 #include <linux/uaccess.h>
51
52 #include <rdma/ib.h>
53 #include <rdma/uverbs_std_types.h>
54 #include <rdma/rdma_netlink.h>
55 #include <rdma/ib_ucaps.h>
56
57 #include "uverbs.h"
58 #include "core_priv.h"
59 #include "rdma_core.h"
60
61 MODULE_AUTHOR("Roland Dreier");
62 MODULE_DESCRIPTION("InfiniBand userspace verbs access");
63 MODULE_LICENSE("Dual BSD/GPL");
64 MODULE_IMPORT_NS("rdma_core");
65
66 enum {
67 IB_UVERBS_MAJOR = 231,
68 IB_UVERBS_BASE_MINOR = 192,
69 IB_UVERBS_MAX_DEVICES = RDMA_MAX_PORTS,
70 IB_UVERBS_NUM_FIXED_MINOR = 32,
71 IB_UVERBS_NUM_DYNAMIC_MINOR = IB_UVERBS_MAX_DEVICES - IB_UVERBS_NUM_FIXED_MINOR,
72 };
73
74 #define IB_UVERBS_BASE_DEV MKDEV(IB_UVERBS_MAJOR, IB_UVERBS_BASE_MINOR)
75
76 static dev_t dynamic_uverbs_dev;
77
78 static DEFINE_IDA(uverbs_ida);
79 static int ib_uverbs_add_one(struct ib_device *device);
80 static void ib_uverbs_remove_one(struct ib_device *device, void *client_data);
81 static struct ib_client uverbs_client;
82
uverbs_devnode(const struct device * dev,umode_t * mode)83 static char *uverbs_devnode(const struct device *dev, umode_t *mode)
84 {
85 if (mode)
86 *mode = 0666;
87 return kasprintf(GFP_KERNEL, "infiniband/%s", dev_name(dev));
88 }
89
90 static const struct class uverbs_class = {
91 .name = "infiniband_verbs",
92 .devnode = uverbs_devnode,
93 };
94
uverbs_dealloc_mw(struct ib_mw * mw)95 int uverbs_dealloc_mw(struct ib_mw *mw)
96 {
97 struct ib_pd *pd = mw->pd;
98 int ret;
99
100 ret = mw->device->ops.dealloc_mw(mw);
101 if (ret)
102 return ret;
103
104 atomic_dec(&pd->usecnt);
105 kfree(mw);
106 return ret;
107 }
108
ib_uverbs_release_dev(struct device * device)109 static void ib_uverbs_release_dev(struct device *device)
110 {
111 struct ib_uverbs_device *dev =
112 container_of(device, struct ib_uverbs_device, dev);
113
114 uverbs_destroy_api(dev->uapi);
115 cleanup_srcu_struct(&dev->disassociate_srcu);
116 mutex_destroy(&dev->lists_mutex);
117 mutex_destroy(&dev->xrcd_tree_mutex);
118 kfree(dev);
119 }
120
ib_uverbs_release_ucq(struct ib_uverbs_completion_event_file * ev_file,struct ib_ucq_object * uobj)121 void ib_uverbs_release_ucq(struct ib_uverbs_completion_event_file *ev_file,
122 struct ib_ucq_object *uobj)
123 {
124 struct ib_uverbs_event *evt, *tmp;
125
126 if (ev_file) {
127 spin_lock_irq(&ev_file->ev_queue.lock);
128 list_for_each_entry_safe(evt, tmp, &uobj->comp_list, obj_list) {
129 list_del(&evt->list);
130 kfree(evt);
131 }
132 spin_unlock_irq(&ev_file->ev_queue.lock);
133
134 uverbs_uobject_put(&ev_file->uobj);
135 }
136
137 ib_uverbs_release_uevent(&uobj->uevent);
138 }
139
ib_uverbs_release_uevent(struct ib_uevent_object * uobj)140 void ib_uverbs_release_uevent(struct ib_uevent_object *uobj)
141 {
142 struct ib_uverbs_async_event_file *async_file = uobj->event_file;
143 struct ib_uverbs_event *evt, *tmp;
144
145 if (!async_file)
146 return;
147
148 spin_lock_irq(&async_file->ev_queue.lock);
149 list_for_each_entry_safe(evt, tmp, &uobj->event_list, obj_list) {
150 list_del(&evt->list);
151 kfree(evt);
152 }
153 spin_unlock_irq(&async_file->ev_queue.lock);
154 uverbs_uobject_put(&async_file->uobj);
155 }
156
ib_uverbs_detach_umcast(struct ib_qp * qp,struct ib_uqp_object * uobj)157 void ib_uverbs_detach_umcast(struct ib_qp *qp,
158 struct ib_uqp_object *uobj)
159 {
160 struct ib_uverbs_mcast_entry *mcast, *tmp;
161
162 list_for_each_entry_safe(mcast, tmp, &uobj->mcast_list, list) {
163 ib_detach_mcast(qp, &mcast->gid, mcast->lid);
164 list_del(&mcast->list);
165 kfree(mcast);
166 }
167 }
168
ib_uverbs_event_read(struct ib_uverbs_event_queue * ev_queue,struct file * filp,char __user * buf,size_t count,loff_t * pos,size_t eventsz)169 static ssize_t ib_uverbs_event_read(struct ib_uverbs_event_queue *ev_queue,
170 struct file *filp, char __user *buf,
171 size_t count, loff_t *pos,
172 size_t eventsz)
173 {
174 struct ib_uverbs_event *event;
175 int ret = 0;
176
177 spin_lock_irq(&ev_queue->lock);
178
179 while (list_empty(&ev_queue->event_list)) {
180 if (ev_queue->is_closed) {
181 spin_unlock_irq(&ev_queue->lock);
182 return -EIO;
183 }
184
185 spin_unlock_irq(&ev_queue->lock);
186 if (filp->f_flags & O_NONBLOCK)
187 return -EAGAIN;
188
189 if (wait_event_interruptible(ev_queue->poll_wait,
190 (!list_empty(&ev_queue->event_list) ||
191 ev_queue->is_closed)))
192 return -ERESTARTSYS;
193
194 spin_lock_irq(&ev_queue->lock);
195 }
196
197 event = list_entry(ev_queue->event_list.next, struct ib_uverbs_event, list);
198
199 if (eventsz > count) {
200 ret = -EINVAL;
201 event = NULL;
202 } else {
203 list_del(ev_queue->event_list.next);
204 if (event->counter) {
205 ++(*event->counter);
206 list_del(&event->obj_list);
207 }
208 }
209
210 spin_unlock_irq(&ev_queue->lock);
211
212 if (event) {
213 if (copy_to_user(buf, event, eventsz))
214 ret = -EFAULT;
215 else
216 ret = eventsz;
217 }
218
219 kfree(event);
220
221 return ret;
222 }
223
ib_uverbs_async_event_read(struct file * filp,char __user * buf,size_t count,loff_t * pos)224 static ssize_t ib_uverbs_async_event_read(struct file *filp, char __user *buf,
225 size_t count, loff_t *pos)
226 {
227 struct ib_uverbs_async_event_file *file = filp->private_data;
228
229 return ib_uverbs_event_read(&file->ev_queue, filp, buf, count, pos,
230 sizeof(struct ib_uverbs_async_event_desc));
231 }
232
ib_uverbs_comp_event_read(struct file * filp,char __user * buf,size_t count,loff_t * pos)233 static ssize_t ib_uverbs_comp_event_read(struct file *filp, char __user *buf,
234 size_t count, loff_t *pos)
235 {
236 struct ib_uverbs_completion_event_file *comp_ev_file =
237 filp->private_data;
238
239 return ib_uverbs_event_read(&comp_ev_file->ev_queue, filp, buf, count,
240 pos,
241 sizeof(struct ib_uverbs_comp_event_desc));
242 }
243
ib_uverbs_event_poll(struct ib_uverbs_event_queue * ev_queue,struct file * filp,struct poll_table_struct * wait)244 static __poll_t ib_uverbs_event_poll(struct ib_uverbs_event_queue *ev_queue,
245 struct file *filp,
246 struct poll_table_struct *wait)
247 {
248 __poll_t pollflags = 0;
249
250 poll_wait(filp, &ev_queue->poll_wait, wait);
251
252 spin_lock_irq(&ev_queue->lock);
253 if (!list_empty(&ev_queue->event_list))
254 pollflags = EPOLLIN | EPOLLRDNORM;
255 else if (ev_queue->is_closed)
256 pollflags = EPOLLERR;
257 spin_unlock_irq(&ev_queue->lock);
258
259 return pollflags;
260 }
261
ib_uverbs_async_event_poll(struct file * filp,struct poll_table_struct * wait)262 static __poll_t ib_uverbs_async_event_poll(struct file *filp,
263 struct poll_table_struct *wait)
264 {
265 struct ib_uverbs_async_event_file *file = filp->private_data;
266
267 return ib_uverbs_event_poll(&file->ev_queue, filp, wait);
268 }
269
ib_uverbs_comp_event_poll(struct file * filp,struct poll_table_struct * wait)270 static __poll_t ib_uverbs_comp_event_poll(struct file *filp,
271 struct poll_table_struct *wait)
272 {
273 struct ib_uverbs_completion_event_file *comp_ev_file =
274 filp->private_data;
275
276 return ib_uverbs_event_poll(&comp_ev_file->ev_queue, filp, wait);
277 }
278
ib_uverbs_async_event_fasync(int fd,struct file * filp,int on)279 static int ib_uverbs_async_event_fasync(int fd, struct file *filp, int on)
280 {
281 struct ib_uverbs_async_event_file *file = filp->private_data;
282
283 return fasync_helper(fd, filp, on, &file->ev_queue.async_queue);
284 }
285
ib_uverbs_comp_event_fasync(int fd,struct file * filp,int on)286 static int ib_uverbs_comp_event_fasync(int fd, struct file *filp, int on)
287 {
288 struct ib_uverbs_completion_event_file *comp_ev_file =
289 filp->private_data;
290
291 return fasync_helper(fd, filp, on, &comp_ev_file->ev_queue.async_queue);
292 }
293
294 const struct file_operations uverbs_event_fops = {
295 .owner = THIS_MODULE,
296 .read = ib_uverbs_comp_event_read,
297 .poll = ib_uverbs_comp_event_poll,
298 .release = uverbs_uobject_fd_release,
299 .fasync = ib_uverbs_comp_event_fasync,
300 };
301
302 const struct file_operations uverbs_async_event_fops = {
303 .owner = THIS_MODULE,
304 .read = ib_uverbs_async_event_read,
305 .poll = ib_uverbs_async_event_poll,
306 .release = uverbs_uobject_fd_release,
307 .fasync = ib_uverbs_async_event_fasync,
308 };
309
ib_uverbs_comp_handler(struct ib_cq * cq,void * cq_context)310 void ib_uverbs_comp_handler(struct ib_cq *cq, void *cq_context)
311 {
312 struct ib_uverbs_event_queue *ev_queue = cq_context;
313 struct ib_ucq_object *uobj;
314 struct ib_uverbs_event *entry;
315 unsigned long flags;
316
317 if (!ev_queue)
318 return;
319
320 spin_lock_irqsave(&ev_queue->lock, flags);
321 if (ev_queue->is_closed) {
322 spin_unlock_irqrestore(&ev_queue->lock, flags);
323 return;
324 }
325
326 entry = kmalloc_obj(*entry, GFP_ATOMIC);
327 if (!entry) {
328 spin_unlock_irqrestore(&ev_queue->lock, flags);
329 return;
330 }
331
332 uobj = cq->uobject;
333
334 entry->desc.comp.cq_handle = cq->uobject->uevent.uobject.user_handle;
335 entry->counter = &uobj->comp_events_reported;
336
337 list_add_tail(&entry->list, &ev_queue->event_list);
338 list_add_tail(&entry->obj_list, &uobj->comp_list);
339 spin_unlock_irqrestore(&ev_queue->lock, flags);
340
341 wake_up_interruptible(&ev_queue->poll_wait);
342 kill_fasync(&ev_queue->async_queue, SIGIO, POLL_IN);
343 }
344
ib_uverbs_async_handler(struct ib_uverbs_async_event_file * async_file,__u64 element,__u64 event,struct list_head * obj_list,u32 * counter)345 void ib_uverbs_async_handler(struct ib_uverbs_async_event_file *async_file,
346 __u64 element, __u64 event,
347 struct list_head *obj_list, u32 *counter)
348 {
349 struct ib_uverbs_event *entry;
350 unsigned long flags;
351
352 if (!async_file)
353 return;
354
355 spin_lock_irqsave(&async_file->ev_queue.lock, flags);
356 if (async_file->ev_queue.is_closed) {
357 spin_unlock_irqrestore(&async_file->ev_queue.lock, flags);
358 return;
359 }
360
361 entry = kmalloc_obj(*entry, GFP_ATOMIC);
362 if (!entry) {
363 spin_unlock_irqrestore(&async_file->ev_queue.lock, flags);
364 return;
365 }
366
367 entry->desc.async.element = element;
368 entry->desc.async.event_type = event;
369 entry->desc.async.reserved = 0;
370 entry->counter = counter;
371
372 list_add_tail(&entry->list, &async_file->ev_queue.event_list);
373 if (obj_list)
374 list_add_tail(&entry->obj_list, obj_list);
375 spin_unlock_irqrestore(&async_file->ev_queue.lock, flags);
376
377 wake_up_interruptible(&async_file->ev_queue.poll_wait);
378 kill_fasync(&async_file->ev_queue.async_queue, SIGIO, POLL_IN);
379 }
380
uverbs_uobj_event(struct ib_uevent_object * eobj,struct ib_event * event)381 static void uverbs_uobj_event(struct ib_uevent_object *eobj,
382 struct ib_event *event)
383 {
384 ib_uverbs_async_handler(eobj->event_file,
385 eobj->uobject.user_handle, event->event,
386 &eobj->event_list, &eobj->events_reported);
387 }
388
ib_uverbs_cq_event_handler(struct ib_event * event,void * context_ptr)389 void ib_uverbs_cq_event_handler(struct ib_event *event, void *context_ptr)
390 {
391 uverbs_uobj_event(&event->element.cq->uobject->uevent, event);
392 }
393
ib_uverbs_qp_event_handler(struct ib_event * event,void * context_ptr)394 void ib_uverbs_qp_event_handler(struct ib_event *event, void *context_ptr)
395 {
396 /* for XRC target qp's, check that qp is live */
397 if (!event->element.qp->uobject)
398 return;
399
400 uverbs_uobj_event(&event->element.qp->uobject->uevent, event);
401 }
402
ib_uverbs_wq_event_handler(struct ib_event * event,void * context_ptr)403 void ib_uverbs_wq_event_handler(struct ib_event *event, void *context_ptr)
404 {
405 uverbs_uobj_event(&event->element.wq->uobject->uevent, event);
406 }
407
ib_uverbs_srq_event_handler(struct ib_event * event,void * context_ptr)408 void ib_uverbs_srq_event_handler(struct ib_event *event, void *context_ptr)
409 {
410 uverbs_uobj_event(&event->element.srq->uobject->uevent, event);
411 }
412
ib_uverbs_event_handler(struct ib_event_handler * handler,struct ib_event * event)413 static void ib_uverbs_event_handler(struct ib_event_handler *handler,
414 struct ib_event *event)
415 {
416 ib_uverbs_async_handler(
417 container_of(handler, struct ib_uverbs_async_event_file,
418 event_handler),
419 event->element.port_num, event->event, NULL, NULL);
420 }
421
ib_uverbs_init_event_queue(struct ib_uverbs_event_queue * ev_queue)422 void ib_uverbs_init_event_queue(struct ib_uverbs_event_queue *ev_queue)
423 {
424 spin_lock_init(&ev_queue->lock);
425 INIT_LIST_HEAD(&ev_queue->event_list);
426 init_waitqueue_head(&ev_queue->poll_wait);
427 ev_queue->is_closed = 0;
428 ev_queue->async_queue = NULL;
429 }
430
ib_uverbs_init_async_event_file(struct ib_uverbs_async_event_file * async_file)431 void ib_uverbs_init_async_event_file(
432 struct ib_uverbs_async_event_file *async_file)
433 {
434 struct ib_uverbs_file *uverbs_file = async_file->uobj.ufile;
435 struct ib_device *ib_dev = async_file->uobj.context->device;
436
437 ib_uverbs_init_event_queue(&async_file->ev_queue);
438
439 /* The first async_event_file becomes the default one for the file. */
440 mutex_lock(&uverbs_file->ucontext_lock);
441 if (!uverbs_file->default_async_file) {
442 /* Pairs with the put in ib_uverbs_release_file */
443 uverbs_uobject_get(&async_file->uobj);
444 smp_store_release(&uverbs_file->default_async_file, async_file);
445 }
446 mutex_unlock(&uverbs_file->ucontext_lock);
447
448 INIT_IB_EVENT_HANDLER(&async_file->event_handler, ib_dev,
449 ib_uverbs_event_handler);
450 ib_register_event_handler(&async_file->event_handler);
451 }
452
verify_hdr(struct ib_uverbs_cmd_hdr * hdr,struct ib_uverbs_ex_cmd_hdr * ex_hdr,size_t count,const struct uverbs_api_write_method * method_elm)453 static ssize_t verify_hdr(struct ib_uverbs_cmd_hdr *hdr,
454 struct ib_uverbs_ex_cmd_hdr *ex_hdr, size_t count,
455 const struct uverbs_api_write_method *method_elm)
456 {
457 if (method_elm->is_ex) {
458 count -= sizeof(*hdr) + sizeof(*ex_hdr);
459
460 if ((hdr->in_words + ex_hdr->provider_in_words) * 8 != count)
461 return -EINVAL;
462
463 if (hdr->in_words * 8 < method_elm->req_size)
464 return -ENOSPC;
465
466 if (ex_hdr->cmd_hdr_reserved)
467 return -EINVAL;
468
469 if (ex_hdr->response) {
470 if (!hdr->out_words && !ex_hdr->provider_out_words)
471 return -EINVAL;
472
473 if (hdr->out_words * 8 < method_elm->resp_size)
474 return -ENOSPC;
475
476 if (!access_ok(u64_to_user_ptr(ex_hdr->response),
477 (hdr->out_words + ex_hdr->provider_out_words) * 8))
478 return -EFAULT;
479 } else {
480 if (hdr->out_words || ex_hdr->provider_out_words)
481 return -EINVAL;
482 }
483
484 return 0;
485 }
486
487 /* not extended command */
488 if (hdr->in_words * 4 != count)
489 return -EINVAL;
490
491 if (count < method_elm->req_size + sizeof(*hdr)) {
492 /*
493 * rdma-core v18 and v19 have a bug where they send DESTROY_CQ
494 * with a 16 byte write instead of 24. Old kernels didn't
495 * check the size so they allowed this. Now that the size is
496 * checked provide a compatibility work around to not break
497 * those userspaces.
498 */
499 if (hdr->command == IB_USER_VERBS_CMD_DESTROY_CQ &&
500 count == 16) {
501 hdr->in_words = 6;
502 return 0;
503 }
504 return -ENOSPC;
505 }
506 if (hdr->out_words * 4 < method_elm->resp_size)
507 return -ENOSPC;
508
509 return 0;
510 }
511
ib_uverbs_write(struct file * filp,const char __user * buf,size_t count,loff_t * pos)512 static ssize_t ib_uverbs_write(struct file *filp, const char __user *buf,
513 size_t count, loff_t *pos)
514 {
515 struct ib_uverbs_file *file = filp->private_data;
516 const struct uverbs_api_write_method *method_elm;
517 struct uverbs_api *uapi = file->device->uapi;
518 struct ib_uverbs_ex_cmd_hdr ex_hdr;
519 struct ib_uverbs_cmd_hdr hdr;
520 struct uverbs_attr_bundle bundle;
521 int srcu_key;
522 ssize_t ret;
523
524 if (!ib_safe_file_access(filp)) {
525 pr_err_once("uverbs_write: process %d (%s) changed security contexts after opening file descriptor, this is not allowed.\n",
526 task_tgid_vnr(current), current->comm);
527 return -EACCES;
528 }
529
530 if (count < sizeof(hdr))
531 return -EINVAL;
532
533 if (copy_from_user(&hdr, buf, sizeof(hdr)))
534 return -EFAULT;
535
536 method_elm = uapi_get_method(uapi, hdr.command);
537 if (IS_ERR(method_elm))
538 return PTR_ERR(method_elm);
539
540 if (method_elm->is_ex) {
541 if (count < (sizeof(hdr) + sizeof(ex_hdr)))
542 return -EINVAL;
543 if (copy_from_user(&ex_hdr, buf + sizeof(hdr), sizeof(ex_hdr)))
544 return -EFAULT;
545 }
546
547 ret = verify_hdr(&hdr, &ex_hdr, count, method_elm);
548 if (ret)
549 return ret;
550
551 srcu_key = srcu_read_lock(&file->device->disassociate_srcu);
552
553 buf += sizeof(hdr);
554
555 memset(bundle.attr_present, 0, sizeof(bundle.attr_present));
556 bundle.ufile = file;
557 bundle.context = NULL; /* only valid if bundle has uobject */
558 bundle.uobject = NULL;
559 bundle.method_elm = NULL;
560 if (!method_elm->is_ex) {
561 size_t in_len = hdr.in_words * 4 - sizeof(hdr);
562 size_t out_len = hdr.out_words * 4;
563 u64 response = 0;
564
565 if (method_elm->has_udata) {
566 bundle.driver_udata.inlen =
567 in_len - method_elm->req_size;
568 in_len = method_elm->req_size;
569 if (bundle.driver_udata.inlen)
570 bundle.driver_udata.inbuf = buf + in_len;
571 else
572 bundle.driver_udata.inbuf = NULL;
573 } else {
574 memset(&bundle.driver_udata, 0,
575 sizeof(bundle.driver_udata));
576 }
577
578 if (method_elm->has_resp) {
579 /*
580 * The macros check that if has_resp is set
581 * then the command request structure starts
582 * with a '__aligned u64 response' member.
583 */
584 ret = get_user(response, (const u64 __user *)buf);
585 if (ret)
586 goto out_unlock;
587
588 if (method_elm->has_udata) {
589 bundle.driver_udata.outlen =
590 out_len - method_elm->resp_size;
591 out_len = method_elm->resp_size;
592 if (bundle.driver_udata.outlen)
593 bundle.driver_udata.outbuf =
594 u64_to_user_ptr(response +
595 out_len);
596 else
597 bundle.driver_udata.outbuf = NULL;
598 }
599 } else {
600 bundle.driver_udata.outlen = 0;
601 bundle.driver_udata.outbuf = NULL;
602 }
603
604 ib_uverbs_init_udata_buf_or_null(
605 &bundle.ucore, buf, u64_to_user_ptr(response),
606 in_len, out_len);
607 } else {
608 buf += sizeof(ex_hdr);
609
610 ib_uverbs_init_udata_buf_or_null(&bundle.ucore, buf,
611 u64_to_user_ptr(ex_hdr.response),
612 hdr.in_words * 8, hdr.out_words * 8);
613
614 ib_uverbs_init_udata_buf_or_null(
615 &bundle.driver_udata, buf + bundle.ucore.inlen,
616 u64_to_user_ptr(ex_hdr.response) + bundle.ucore.outlen,
617 ex_hdr.provider_in_words * 8,
618 ex_hdr.provider_out_words * 8);
619
620 }
621
622 ret = method_elm->handler(&bundle);
623 if (bundle.uobject)
624 uverbs_finalize_object(bundle.uobject, UVERBS_ACCESS_NEW, true,
625 !ret, &bundle);
626 out_unlock:
627 srcu_read_unlock(&file->device->disassociate_srcu, srcu_key);
628 return (ret) ? : count;
629 }
630
631 static const struct vm_operations_struct rdma_umap_ops;
632
ib_uverbs_mmap(struct file * filp,struct vm_area_struct * vma)633 static int ib_uverbs_mmap(struct file *filp, struct vm_area_struct *vma)
634 {
635 struct ib_uverbs_file *file = filp->private_data;
636 struct ib_ucontext *ucontext;
637 int ret = 0;
638 int srcu_key;
639
640 srcu_key = srcu_read_lock(&file->device->disassociate_srcu);
641 ucontext = ib_uverbs_get_ucontext_file(file);
642 if (IS_ERR(ucontext)) {
643 ret = PTR_ERR(ucontext);
644 goto out;
645 }
646
647 if (!down_read_trylock(&file->hw_destroy_rwsem)) {
648 ret = -EIO;
649 goto out;
650 }
651
652 vma->vm_ops = &rdma_umap_ops;
653 ret = ucontext->device->ops.mmap(ucontext, vma);
654
655 up_read(&file->hw_destroy_rwsem);
656 out:
657 srcu_read_unlock(&file->device->disassociate_srcu, srcu_key);
658 return ret;
659 }
660
661 /*
662 * The VMA has been dup'd, initialize the vm_private_data with a new tracking
663 * struct
664 */
rdma_umap_open(struct vm_area_struct * vma)665 static void rdma_umap_open(struct vm_area_struct *vma)
666 {
667 struct ib_uverbs_file *ufile = vma->vm_file->private_data;
668 struct rdma_umap_priv *opriv = vma->vm_private_data;
669 struct rdma_umap_priv *priv;
670
671 if (!opriv)
672 return;
673
674 /* We are racing with disassociation */
675 if (!down_read_trylock(&ufile->hw_destroy_rwsem))
676 goto out_zap;
677
678 /*
679 * Disassociation already completed, the VMA should already be zapped.
680 */
681 if (!ufile->ucontext)
682 goto out_unlock;
683
684 priv = kzalloc_obj(*priv);
685 if (!priv)
686 goto out_unlock;
687 rdma_umap_priv_init(priv, vma, opriv->entry);
688
689 up_read(&ufile->hw_destroy_rwsem);
690 return;
691
692 out_unlock:
693 up_read(&ufile->hw_destroy_rwsem);
694 out_zap:
695 /*
696 * We can't allow the VMA to be created with the actual IO pages, that
697 * would break our API contract, and it can't be stopped at this
698 * point, so zap it.
699 */
700 vma->vm_private_data = NULL;
701 zap_special_vma_range(vma, vma->vm_start, vma->vm_end - vma->vm_start);
702 }
703
rdma_umap_close(struct vm_area_struct * vma)704 static void rdma_umap_close(struct vm_area_struct *vma)
705 {
706 struct ib_uverbs_file *ufile = vma->vm_file->private_data;
707 struct rdma_umap_priv *priv = vma->vm_private_data;
708
709 if (!priv)
710 return;
711
712 /*
713 * The vma holds a reference on the struct file that created it, which
714 * in turn means that the ib_uverbs_file is guaranteed to exist at
715 * this point.
716 */
717 mutex_lock(&ufile->umap_lock);
718 if (priv->entry)
719 rdma_user_mmap_entry_put(priv->entry);
720
721 list_del(&priv->list);
722 mutex_unlock(&ufile->umap_lock);
723 kfree(priv);
724 }
725
726 /*
727 * Once the zap_special_vma_range has been called touches to the VMA will come here and
728 * we return a dummy writable zero page for all the pfns.
729 */
rdma_umap_fault(struct vm_fault * vmf)730 static vm_fault_t rdma_umap_fault(struct vm_fault *vmf)
731 {
732 struct ib_uverbs_file *ufile = vmf->vma->vm_file->private_data;
733 struct rdma_umap_priv *priv = vmf->vma->vm_private_data;
734 vm_fault_t ret = 0;
735
736 if (!priv)
737 return VM_FAULT_SIGBUS;
738
739 /* Read only pages can just use the system zero page. */
740 if (!(vmf->vma->vm_flags & (VM_WRITE | VM_MAYWRITE))) {
741 vmf->page = ZERO_PAGE(vmf->address);
742 get_page(vmf->page);
743 return 0;
744 }
745
746 mutex_lock(&ufile->umap_lock);
747 if (!ufile->disassociate_page)
748 ufile->disassociate_page =
749 alloc_pages(vmf->gfp_mask | __GFP_ZERO, 0);
750
751 if (ufile->disassociate_page) {
752 /*
753 * This VMA is forced to always be shared so this doesn't have
754 * to worry about COW.
755 */
756 vmf->page = ufile->disassociate_page;
757 get_page(vmf->page);
758 } else {
759 ret = VM_FAULT_SIGBUS;
760 }
761 mutex_unlock(&ufile->umap_lock);
762
763 return ret;
764 }
765
766 static const struct vm_operations_struct rdma_umap_ops = {
767 .open = rdma_umap_open,
768 .close = rdma_umap_close,
769 .fault = rdma_umap_fault,
770 };
771
uverbs_user_mmap_disassociate(struct ib_uverbs_file * ufile)772 void uverbs_user_mmap_disassociate(struct ib_uverbs_file *ufile)
773 {
774 struct rdma_umap_priv *priv, *next_priv;
775
776 lockdep_assert_held_write(&ufile->hw_destroy_rwsem);
777
778 while (1) {
779 struct mm_struct *mm = NULL;
780
781 /* Get an arbitrary mm pointer that hasn't been cleaned yet */
782 mutex_lock(&ufile->umap_lock);
783 while (!list_empty(&ufile->umaps)) {
784 int ret;
785
786 priv = list_first_entry(&ufile->umaps,
787 struct rdma_umap_priv, list);
788 mm = priv->vma->vm_mm;
789 ret = mmget_not_zero(mm);
790 if (!ret) {
791 list_del_init(&priv->list);
792 if (priv->entry) {
793 rdma_user_mmap_entry_put(priv->entry);
794 priv->entry = NULL;
795 }
796 mm = NULL;
797 continue;
798 }
799 break;
800 }
801 mutex_unlock(&ufile->umap_lock);
802 if (!mm)
803 return;
804
805 /*
806 * The umap_lock is nested under mmap_lock since it used within
807 * the vma_ops callbacks, so we have to clean the list one mm
808 * at a time to get the lock ordering right. Typically there
809 * will only be one mm, so no big deal.
810 */
811 mmap_read_lock(mm);
812 mutex_lock(&ufile->umap_lock);
813 list_for_each_entry_safe (priv, next_priv, &ufile->umaps,
814 list) {
815 struct vm_area_struct *vma = priv->vma;
816
817 if (vma->vm_mm != mm)
818 continue;
819 list_del_init(&priv->list);
820
821 zap_special_vma_range(vma, vma->vm_start,
822 vma->vm_end - vma->vm_start);
823
824 if (priv->entry) {
825 rdma_user_mmap_entry_put(priv->entry);
826 priv->entry = NULL;
827 }
828 }
829 mutex_unlock(&ufile->umap_lock);
830 mmap_read_unlock(mm);
831 mmput(mm);
832 }
833 }
834
835 /**
836 * rdma_user_mmap_disassociate() - Revoke mmaps for a device
837 * @device: device to revoke
838 *
839 * This function should be called by drivers that need to disable mmaps for the
840 * device, for instance because it is going to be reset.
841 */
rdma_user_mmap_disassociate(struct ib_device * device)842 void rdma_user_mmap_disassociate(struct ib_device *device)
843 {
844 struct ib_uverbs_device *uverbs_dev =
845 ib_get_client_data(device, &uverbs_client);
846 struct ib_uverbs_file *ufile;
847
848 mutex_lock(&uverbs_dev->lists_mutex);
849 list_for_each_entry(ufile, &uverbs_dev->uverbs_file_list, list) {
850 if (ufile->ucontext) {
851 down_write(&ufile->hw_destroy_rwsem);
852 uverbs_user_mmap_disassociate(ufile);
853 up_write(&ufile->hw_destroy_rwsem);
854 }
855 }
856 mutex_unlock(&uverbs_dev->lists_mutex);
857 }
858 EXPORT_SYMBOL(rdma_user_mmap_disassociate);
859
860 /*
861 * ib_uverbs_open() does not need the BKL:
862 *
863 * - the ib_uverbs_device structures are properly reference counted and
864 * everything else is purely local to the file being created, so
865 * races against other open calls are not a problem;
866 * - there is no ioctl method to race against;
867 * - the open method will either immediately run -ENXIO, or all
868 * required initialization will be done.
869 */
ib_uverbs_open(struct inode * inode,struct file * filp)870 static int ib_uverbs_open(struct inode *inode, struct file *filp)
871 {
872 struct ib_uverbs_device *dev;
873 struct ib_uverbs_file *file;
874 struct ib_device *ib_dev;
875 int ret;
876 int module_dependent;
877 int srcu_key;
878
879 dev = container_of(inode->i_cdev, struct ib_uverbs_device, cdev);
880 if (!refcount_inc_not_zero(&dev->refcount))
881 return -ENXIO;
882
883 get_device(&dev->dev);
884 srcu_key = srcu_read_lock(&dev->disassociate_srcu);
885 mutex_lock(&dev->lists_mutex);
886 ib_dev = srcu_dereference(dev->ib_dev,
887 &dev->disassociate_srcu);
888 if (!ib_dev) {
889 ret = -EIO;
890 goto err;
891 }
892
893 if (!rdma_dev_access_netns(ib_dev, current->nsproxy->net_ns)) {
894 ret = -EPERM;
895 goto err;
896 }
897
898 /* In case IB device supports disassociate ucontext, there is no hard
899 * dependency between uverbs device and its low level device.
900 */
901 module_dependent = !(ib_dev->ops.disassociate_ucontext);
902
903 if (module_dependent) {
904 if (!try_module_get(ib_dev->ops.owner)) {
905 ret = -ENODEV;
906 goto err;
907 }
908 }
909
910 file = kzalloc_obj(*file);
911 if (!file) {
912 ret = -ENOMEM;
913 if (module_dependent)
914 goto err_module;
915
916 goto err;
917 }
918
919 file->device = dev;
920 kref_init(&file->ref);
921 mutex_init(&file->ucontext_lock);
922
923 spin_lock_init(&file->uobjects_lock);
924 INIT_LIST_HEAD(&file->uobjects);
925 init_rwsem(&file->hw_destroy_rwsem);
926 mutex_init(&file->umap_lock);
927 INIT_LIST_HEAD(&file->umaps);
928
929 filp->private_data = file;
930 list_add_tail(&file->list, &dev->uverbs_file_list);
931 mutex_unlock(&dev->lists_mutex);
932 srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
933
934 setup_ufile_idr_uobject(file);
935
936 return stream_open(inode, filp);
937
938 err_module:
939 module_put(ib_dev->ops.owner);
940
941 err:
942 mutex_unlock(&dev->lists_mutex);
943 srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
944 if (refcount_dec_and_test(&dev->refcount))
945 ib_uverbs_comp_dev(dev);
946
947 put_device(&dev->dev);
948 return ret;
949 }
950
951 /*
952 * Drop the ucontext off the ufile and completely disconnect it from the
953 * ib_device
954 */
ufile_destroy_ucontext(struct ib_uverbs_file * ufile,enum rdma_remove_reason reason)955 static void ufile_destroy_ucontext(struct ib_uverbs_file *ufile,
956 enum rdma_remove_reason reason)
957 {
958 struct ib_ucontext *ucontext = ufile->ucontext;
959 struct ib_device *ib_dev = ucontext->device;
960
961 /*
962 * If we are closing the FD then the user mmap VMAs must have
963 * already been destroyed as they hold on to the filep, otherwise
964 * they need to be zap'd.
965 */
966 if (reason == RDMA_REMOVE_DRIVER_REMOVE) {
967 uverbs_user_mmap_disassociate(ufile);
968 if (ib_dev->ops.disassociate_ucontext)
969 ib_dev->ops.disassociate_ucontext(ucontext);
970 }
971
972 ib_rdmacg_uncharge(&ucontext->cg_obj, ib_dev,
973 RDMACG_RESOURCE_HCA_HANDLE);
974
975 rdma_restrack_del(&ucontext->res);
976
977 ib_dev->ops.dealloc_ucontext(ucontext);
978 WARN_ON(!xa_empty(&ucontext->mmap_xa));
979 kfree(ucontext);
980
981 ufile->ucontext = NULL;
982 }
983
984 /*
985 * Destroy the ucontext and every uobject associated with it.
986 *
987 * This is internally locked and can be called in parallel from multiple
988 * contexts.
989 */
uverbs_destroy_ufile_hw(struct ib_uverbs_file * ufile,enum rdma_remove_reason reason)990 void uverbs_destroy_ufile_hw(struct ib_uverbs_file *ufile,
991 enum rdma_remove_reason reason)
992 {
993 down_write(&ufile->hw_destroy_rwsem);
994
995 /*
996 * If a ucontext was never created then we can't have any uobjects to
997 * cleanup, nothing to do.
998 */
999 if (!ufile->ucontext)
1000 goto done;
1001
1002 while (!list_empty(&ufile->uobjects) &&
1003 !__uverbs_cleanup_ufile(ufile, reason)) {
1004 }
1005
1006 if (WARN_ON(!list_empty(&ufile->uobjects)))
1007 __uverbs_cleanup_ufile(ufile, RDMA_REMOVE_DRIVER_FAILURE);
1008 ufile_destroy_ucontext(ufile, reason);
1009
1010 done:
1011 up_write(&ufile->hw_destroy_rwsem);
1012 }
1013
ib_uverbs_close(struct inode * inode,struct file * filp)1014 static int ib_uverbs_close(struct inode *inode, struct file *filp)
1015 {
1016 struct ib_uverbs_file *file = filp->private_data;
1017
1018 uverbs_destroy_ufile_hw(file, RDMA_REMOVE_CLOSE);
1019
1020 mutex_lock(&file->device->lists_mutex);
1021 list_del_init(&file->list);
1022 mutex_unlock(&file->device->lists_mutex);
1023
1024 kref_put(&file->ref, ib_uverbs_release_file);
1025
1026 return 0;
1027 }
1028
1029 static const struct file_operations uverbs_fops = {
1030 .owner = THIS_MODULE,
1031 .write = ib_uverbs_write,
1032 .open = ib_uverbs_open,
1033 .release = ib_uverbs_close,
1034 .unlocked_ioctl = ib_uverbs_ioctl,
1035 .compat_ioctl = compat_ptr_ioctl,
1036 };
1037
1038 static const struct file_operations uverbs_mmap_fops = {
1039 .owner = THIS_MODULE,
1040 .write = ib_uverbs_write,
1041 .mmap = ib_uverbs_mmap,
1042 .open = ib_uverbs_open,
1043 .release = ib_uverbs_close,
1044 .unlocked_ioctl = ib_uverbs_ioctl,
1045 .compat_ioctl = compat_ptr_ioctl,
1046 };
1047
ib_uverbs_get_nl_info(struct ib_device * ibdev,void * client_data,struct ib_client_nl_info * res)1048 static int ib_uverbs_get_nl_info(struct ib_device *ibdev, void *client_data,
1049 struct ib_client_nl_info *res)
1050 {
1051 struct ib_uverbs_device *uverbs_dev = client_data;
1052 int ret;
1053
1054 if (res->port != -1)
1055 return -EINVAL;
1056
1057 res->abi = ibdev->ops.uverbs_abi_ver;
1058 res->cdev = &uverbs_dev->dev;
1059
1060 /*
1061 * To support DRIVER_ID binding in userspace some of the driver need
1062 * upgrading to expose their PCI dependent revision information
1063 * through get_context instead of relying on modalias matching. When
1064 * the drivers are fixed they can drop this flag.
1065 */
1066 if (!ibdev->ops.uverbs_no_driver_id_binding) {
1067 ret = nla_put_u32(res->nl_msg, RDMA_NLDEV_ATTR_UVERBS_DRIVER_ID,
1068 ibdev->ops.driver_id);
1069 if (ret)
1070 return ret;
1071 }
1072 return 0;
1073 }
1074
1075 static struct ib_client uverbs_client = {
1076 .name = "uverbs",
1077 .no_kverbs_req = true,
1078 .add = ib_uverbs_add_one,
1079 .remove = ib_uverbs_remove_one,
1080 .get_nl_info = ib_uverbs_get_nl_info,
1081 };
1082 MODULE_ALIAS_RDMA_CLIENT("uverbs");
1083
ibdev_show(struct device * device,struct device_attribute * attr,char * buf)1084 static ssize_t ibdev_show(struct device *device, struct device_attribute *attr,
1085 char *buf)
1086 {
1087 struct ib_uverbs_device *dev =
1088 container_of(device, struct ib_uverbs_device, dev);
1089 int ret = -ENODEV;
1090 int srcu_key;
1091 struct ib_device *ib_dev;
1092
1093 srcu_key = srcu_read_lock(&dev->disassociate_srcu);
1094 ib_dev = srcu_dereference(dev->ib_dev, &dev->disassociate_srcu);
1095 if (ib_dev)
1096 ret = sysfs_emit(buf, "%s\n", dev_name(&ib_dev->dev));
1097 srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
1098
1099 return ret;
1100 }
1101 static DEVICE_ATTR_RO(ibdev);
1102
abi_version_show(struct device * device,struct device_attribute * attr,char * buf)1103 static ssize_t abi_version_show(struct device *device,
1104 struct device_attribute *attr, char *buf)
1105 {
1106 struct ib_uverbs_device *dev =
1107 container_of(device, struct ib_uverbs_device, dev);
1108 int ret = -ENODEV;
1109 int srcu_key;
1110 struct ib_device *ib_dev;
1111
1112 srcu_key = srcu_read_lock(&dev->disassociate_srcu);
1113 ib_dev = srcu_dereference(dev->ib_dev, &dev->disassociate_srcu);
1114 if (ib_dev)
1115 ret = sysfs_emit(buf, "%u\n", ib_dev->ops.uverbs_abi_ver);
1116 srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
1117
1118 return ret;
1119 }
1120 static DEVICE_ATTR_RO(abi_version);
1121
1122 static struct attribute *ib_dev_attrs[] = {
1123 &dev_attr_abi_version.attr,
1124 &dev_attr_ibdev.attr,
1125 NULL,
1126 };
1127
1128 static const struct attribute_group dev_attr_group = {
1129 .attrs = ib_dev_attrs,
1130 };
1131
1132 static CLASS_ATTR_STRING(abi_version, S_IRUGO,
1133 __stringify(IB_USER_VERBS_ABI_VERSION));
1134
ib_uverbs_create_uapi(struct ib_device * device,struct ib_uverbs_device * uverbs_dev)1135 static int ib_uverbs_create_uapi(struct ib_device *device,
1136 struct ib_uverbs_device *uverbs_dev)
1137 {
1138 struct uverbs_api *uapi;
1139
1140 uapi = uverbs_alloc_api(device);
1141 if (IS_ERR(uapi))
1142 return PTR_ERR(uapi);
1143
1144 uverbs_dev->uapi = uapi;
1145 return 0;
1146 }
1147
ib_uverbs_add_one(struct ib_device * device)1148 static int ib_uverbs_add_one(struct ib_device *device)
1149 {
1150 int devnum;
1151 dev_t base;
1152 struct ib_uverbs_device *uverbs_dev;
1153 int ret;
1154
1155 if (!device->ops.alloc_ucontext ||
1156 device->type == RDMA_DEVICE_TYPE_SMI)
1157 return -EOPNOTSUPP;
1158
1159 uverbs_dev = kzalloc_obj(*uverbs_dev);
1160 if (!uverbs_dev)
1161 return -ENOMEM;
1162
1163 ret = init_srcu_struct(&uverbs_dev->disassociate_srcu);
1164 if (ret) {
1165 kfree(uverbs_dev);
1166 return -ENOMEM;
1167 }
1168
1169 device_initialize(&uverbs_dev->dev);
1170 uverbs_dev->dev.class = &uverbs_class;
1171 uverbs_dev->dev.parent = device->dev.parent;
1172 uverbs_dev->dev.release = ib_uverbs_release_dev;
1173 uverbs_dev->groups[0] = &dev_attr_group;
1174 uverbs_dev->dev.groups = uverbs_dev->groups;
1175 refcount_set(&uverbs_dev->refcount, 1);
1176 init_completion(&uverbs_dev->comp);
1177 uverbs_dev->xrcd_tree = RB_ROOT;
1178 mutex_init(&uverbs_dev->xrcd_tree_mutex);
1179 mutex_init(&uverbs_dev->lists_mutex);
1180 INIT_LIST_HEAD(&uverbs_dev->uverbs_file_list);
1181 rcu_assign_pointer(uverbs_dev->ib_dev, device);
1182 uverbs_dev->num_comp_vectors = device->num_comp_vectors;
1183
1184 devnum = ida_alloc_max(&uverbs_ida, IB_UVERBS_MAX_DEVICES - 1,
1185 GFP_KERNEL);
1186 if (devnum < 0) {
1187 ret = -ENOMEM;
1188 goto err;
1189 }
1190 uverbs_dev->devnum = devnum;
1191 if (devnum >= IB_UVERBS_NUM_FIXED_MINOR)
1192 base = dynamic_uverbs_dev + devnum - IB_UVERBS_NUM_FIXED_MINOR;
1193 else
1194 base = IB_UVERBS_BASE_DEV + devnum;
1195
1196 ret = ib_uverbs_create_uapi(device, uverbs_dev);
1197 if (ret)
1198 goto err_uapi;
1199
1200 uverbs_dev->dev.devt = base;
1201 dev_set_name(&uverbs_dev->dev, "uverbs%d", uverbs_dev->devnum);
1202
1203 cdev_init(&uverbs_dev->cdev,
1204 device->ops.mmap ? &uverbs_mmap_fops : &uverbs_fops);
1205 uverbs_dev->cdev.owner = THIS_MODULE;
1206
1207 ret = cdev_device_add(&uverbs_dev->cdev, &uverbs_dev->dev);
1208 if (ret)
1209 goto err_uapi;
1210
1211 ib_set_client_data(device, &uverbs_client, uverbs_dev);
1212 return 0;
1213
1214 err_uapi:
1215 ida_free(&uverbs_ida, devnum);
1216 err:
1217 if (refcount_dec_and_test(&uverbs_dev->refcount))
1218 ib_uverbs_comp_dev(uverbs_dev);
1219 wait_for_completion(&uverbs_dev->comp);
1220 put_device(&uverbs_dev->dev);
1221 return ret;
1222 }
1223
ib_uverbs_free_hw_resources(struct ib_uverbs_device * uverbs_dev,struct ib_device * ib_dev)1224 static void ib_uverbs_free_hw_resources(struct ib_uverbs_device *uverbs_dev,
1225 struct ib_device *ib_dev)
1226 {
1227 struct ib_uverbs_file *file;
1228
1229 /* Pending running commands to terminate */
1230 uverbs_disassociate_api_pre(uverbs_dev);
1231
1232 mutex_lock(&uverbs_dev->lists_mutex);
1233 while (!list_empty(&uverbs_dev->uverbs_file_list)) {
1234 file = list_first_entry(&uverbs_dev->uverbs_file_list,
1235 struct ib_uverbs_file, list);
1236 list_del_init(&file->list);
1237 kref_get(&file->ref);
1238
1239 /* We must release the mutex before going ahead and calling
1240 * uverbs_cleanup_ufile, as it might end up indirectly calling
1241 * uverbs_close, for example due to freeing the resources (e.g
1242 * mmput).
1243 */
1244 mutex_unlock(&uverbs_dev->lists_mutex);
1245
1246 uverbs_destroy_ufile_hw(file, RDMA_REMOVE_DRIVER_REMOVE);
1247 kref_put(&file->ref, ib_uverbs_release_file);
1248
1249 mutex_lock(&uverbs_dev->lists_mutex);
1250 }
1251 mutex_unlock(&uverbs_dev->lists_mutex);
1252
1253 uverbs_disassociate_api(uverbs_dev->uapi);
1254 }
1255
ib_uverbs_remove_one(struct ib_device * device,void * client_data)1256 static void ib_uverbs_remove_one(struct ib_device *device, void *client_data)
1257 {
1258 struct ib_uverbs_device *uverbs_dev = client_data;
1259 int wait_clients = 1;
1260
1261 cdev_device_del(&uverbs_dev->cdev, &uverbs_dev->dev);
1262 ida_free(&uverbs_ida, uverbs_dev->devnum);
1263
1264 if (device->ops.disassociate_ucontext) {
1265 /* We disassociate HW resources and immediately return.
1266 * Userspace will see a EIO errno for all future access.
1267 * Upon returning, ib_device may be freed internally and is not
1268 * valid any more.
1269 * uverbs_device is still available until all clients close
1270 * their files, then the uverbs device ref count will be zero
1271 * and its resources will be freed.
1272 * Note: At this point no more files can be opened since the
1273 * cdev was deleted, however active clients can still issue
1274 * commands and close their open files.
1275 */
1276 ib_uverbs_free_hw_resources(uverbs_dev, device);
1277 wait_clients = 0;
1278 }
1279
1280 if (refcount_dec_and_test(&uverbs_dev->refcount))
1281 ib_uverbs_comp_dev(uverbs_dev);
1282 if (wait_clients)
1283 wait_for_completion(&uverbs_dev->comp);
1284
1285 put_device(&uverbs_dev->dev);
1286 }
1287
ib_uverbs_init(void)1288 static int __init ib_uverbs_init(void)
1289 {
1290 int ret;
1291
1292 ret = register_chrdev_region(IB_UVERBS_BASE_DEV,
1293 IB_UVERBS_NUM_FIXED_MINOR,
1294 "infiniband_verbs");
1295 if (ret) {
1296 pr_err("user_verbs: couldn't register device number\n");
1297 goto out;
1298 }
1299
1300 ret = alloc_chrdev_region(&dynamic_uverbs_dev, 0,
1301 IB_UVERBS_NUM_DYNAMIC_MINOR,
1302 "infiniband_verbs");
1303 if (ret) {
1304 pr_err("couldn't register dynamic device number\n");
1305 goto out_alloc;
1306 }
1307
1308 ret = class_register(&uverbs_class);
1309 if (ret) {
1310 pr_err("user_verbs: couldn't create class infiniband_verbs\n");
1311 goto out_chrdev;
1312 }
1313
1314 ret = class_create_file(&uverbs_class, &class_attr_abi_version.attr);
1315 if (ret) {
1316 pr_err("user_verbs: couldn't create abi_version attribute\n");
1317 goto out_class;
1318 }
1319
1320 ret = ib_register_client(&uverbs_client);
1321 if (ret) {
1322 pr_err("user_verbs: couldn't register client\n");
1323 goto out_class;
1324 }
1325
1326 return 0;
1327
1328 out_class:
1329 class_unregister(&uverbs_class);
1330
1331 out_chrdev:
1332 unregister_chrdev_region(dynamic_uverbs_dev,
1333 IB_UVERBS_NUM_DYNAMIC_MINOR);
1334
1335 out_alloc:
1336 unregister_chrdev_region(IB_UVERBS_BASE_DEV,
1337 IB_UVERBS_NUM_FIXED_MINOR);
1338
1339 out:
1340 return ret;
1341 }
1342
ib_uverbs_cleanup(void)1343 static void __exit ib_uverbs_cleanup(void)
1344 {
1345 ib_unregister_client(&uverbs_client);
1346 class_unregister(&uverbs_class);
1347 unregister_chrdev_region(IB_UVERBS_BASE_DEV,
1348 IB_UVERBS_NUM_FIXED_MINOR);
1349 unregister_chrdev_region(dynamic_uverbs_dev,
1350 IB_UVERBS_NUM_DYNAMIC_MINOR);
1351 mmu_notifier_synchronize();
1352 }
1353
1354 module_init(ib_uverbs_init);
1355 module_exit(ib_uverbs_cleanup);
1356