xref: /linux/drivers/infiniband/core/uverbs_main.c (revision bba2c3615bd6cfee7456d1130f2e6b01b3f4e9ba)
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 	if (!method_elm->is_ex) {
560 		size_t in_len = hdr.in_words * 4 - sizeof(hdr);
561 		size_t out_len = hdr.out_words * 4;
562 		u64 response = 0;
563 
564 		if (method_elm->has_udata) {
565 			bundle.driver_udata.inlen =
566 				in_len - method_elm->req_size;
567 			in_len = method_elm->req_size;
568 			if (bundle.driver_udata.inlen)
569 				bundle.driver_udata.inbuf = buf + in_len;
570 			else
571 				bundle.driver_udata.inbuf = NULL;
572 		} else {
573 			memset(&bundle.driver_udata, 0,
574 			       sizeof(bundle.driver_udata));
575 		}
576 
577 		if (method_elm->has_resp) {
578 			/*
579 			 * The macros check that if has_resp is set
580 			 * then the command request structure starts
581 			 * with a '__aligned u64 response' member.
582 			 */
583 			ret = get_user(response, (const u64 __user *)buf);
584 			if (ret)
585 				goto out_unlock;
586 
587 			if (method_elm->has_udata) {
588 				bundle.driver_udata.outlen =
589 					out_len - method_elm->resp_size;
590 				out_len = method_elm->resp_size;
591 				if (bundle.driver_udata.outlen)
592 					bundle.driver_udata.outbuf =
593 						u64_to_user_ptr(response +
594 								out_len);
595 				else
596 					bundle.driver_udata.outbuf = NULL;
597 			}
598 		} else {
599 			bundle.driver_udata.outlen = 0;
600 			bundle.driver_udata.outbuf = NULL;
601 		}
602 
603 		ib_uverbs_init_udata_buf_or_null(
604 			&bundle.ucore, buf, u64_to_user_ptr(response),
605 			in_len, out_len);
606 	} else {
607 		buf += sizeof(ex_hdr);
608 
609 		ib_uverbs_init_udata_buf_or_null(&bundle.ucore, buf,
610 					u64_to_user_ptr(ex_hdr.response),
611 					hdr.in_words * 8, hdr.out_words * 8);
612 
613 		ib_uverbs_init_udata_buf_or_null(
614 			&bundle.driver_udata, buf + bundle.ucore.inlen,
615 			u64_to_user_ptr(ex_hdr.response) + bundle.ucore.outlen,
616 			ex_hdr.provider_in_words * 8,
617 			ex_hdr.provider_out_words * 8);
618 
619 	}
620 
621 	ret = method_elm->handler(&bundle);
622 	if (bundle.uobject)
623 		uverbs_finalize_object(bundle.uobject, UVERBS_ACCESS_NEW, true,
624 				       !ret, &bundle);
625 out_unlock:
626 	srcu_read_unlock(&file->device->disassociate_srcu, srcu_key);
627 	return (ret) ? : count;
628 }
629 
630 static const struct vm_operations_struct rdma_umap_ops;
631 
632 static int ib_uverbs_mmap(struct file *filp, struct vm_area_struct *vma)
633 {
634 	struct ib_uverbs_file *file = filp->private_data;
635 	struct ib_ucontext *ucontext;
636 	int ret = 0;
637 	int srcu_key;
638 
639 	srcu_key = srcu_read_lock(&file->device->disassociate_srcu);
640 	ucontext = ib_uverbs_get_ucontext_file(file);
641 	if (IS_ERR(ucontext)) {
642 		ret = PTR_ERR(ucontext);
643 		goto out;
644 	}
645 
646 	mutex_lock(&file->disassociation_lock);
647 
648 	vma->vm_ops = &rdma_umap_ops;
649 	ret = ucontext->device->ops.mmap(ucontext, vma);
650 
651 	mutex_unlock(&file->disassociation_lock);
652 out:
653 	srcu_read_unlock(&file->device->disassociate_srcu, srcu_key);
654 	return ret;
655 }
656 
657 /*
658  * The VMA has been dup'd, initialize the vm_private_data with a new tracking
659  * struct
660  */
661 static void rdma_umap_open(struct vm_area_struct *vma)
662 {
663 	struct ib_uverbs_file *ufile = vma->vm_file->private_data;
664 	struct rdma_umap_priv *opriv = vma->vm_private_data;
665 	struct rdma_umap_priv *priv;
666 
667 	if (!opriv)
668 		return;
669 
670 	/* We are racing with disassociation */
671 	if (!down_read_trylock(&ufile->hw_destroy_rwsem))
672 		goto out_zap;
673 	mutex_lock(&ufile->disassociation_lock);
674 
675 	/*
676 	 * Disassociation already completed, the VMA should already be zapped.
677 	 */
678 	if (!ufile->ucontext)
679 		goto out_unlock;
680 
681 	priv = kzalloc_obj(*priv);
682 	if (!priv)
683 		goto out_unlock;
684 	rdma_umap_priv_init(priv, vma, opriv->entry);
685 
686 	mutex_unlock(&ufile->disassociation_lock);
687 	up_read(&ufile->hw_destroy_rwsem);
688 	return;
689 
690 out_unlock:
691 	mutex_unlock(&ufile->disassociation_lock);
692 	up_read(&ufile->hw_destroy_rwsem);
693 out_zap:
694 	/*
695 	 * We can't allow the VMA to be created with the actual IO pages, that
696 	 * would break our API contract, and it can't be stopped at this
697 	 * point, so zap it.
698 	 */
699 	vma->vm_private_data = NULL;
700 	zap_special_vma_range(vma, vma->vm_start, vma->vm_end - vma->vm_start);
701 }
702 
703 static void rdma_umap_close(struct vm_area_struct *vma)
704 {
705 	struct ib_uverbs_file *ufile = vma->vm_file->private_data;
706 	struct rdma_umap_priv *priv = vma->vm_private_data;
707 
708 	if (!priv)
709 		return;
710 
711 	/*
712 	 * The vma holds a reference on the struct file that created it, which
713 	 * in turn means that the ib_uverbs_file is guaranteed to exist at
714 	 * this point.
715 	 */
716 	mutex_lock(&ufile->umap_lock);
717 	if (priv->entry)
718 		rdma_user_mmap_entry_put(priv->entry);
719 
720 	list_del(&priv->list);
721 	mutex_unlock(&ufile->umap_lock);
722 	kfree(priv);
723 }
724 
725 /*
726  * Once the zap_special_vma_range has been called touches to the VMA will come here and
727  * we return a dummy writable zero page for all the pfns.
728  */
729 static vm_fault_t rdma_umap_fault(struct vm_fault *vmf)
730 {
731 	struct ib_uverbs_file *ufile = vmf->vma->vm_file->private_data;
732 	struct rdma_umap_priv *priv = vmf->vma->vm_private_data;
733 	vm_fault_t ret = 0;
734 
735 	if (!priv)
736 		return VM_FAULT_SIGBUS;
737 
738 	/* Read only pages can just use the system zero page. */
739 	if (!(vmf->vma->vm_flags & (VM_WRITE | VM_MAYWRITE))) {
740 		vmf->page = ZERO_PAGE(vmf->address);
741 		get_page(vmf->page);
742 		return 0;
743 	}
744 
745 	mutex_lock(&ufile->umap_lock);
746 	if (!ufile->disassociate_page)
747 		ufile->disassociate_page =
748 			alloc_pages(vmf->gfp_mask | __GFP_ZERO, 0);
749 
750 	if (ufile->disassociate_page) {
751 		/*
752 		 * This VMA is forced to always be shared so this doesn't have
753 		 * to worry about COW.
754 		 */
755 		vmf->page = ufile->disassociate_page;
756 		get_page(vmf->page);
757 	} else {
758 		ret = VM_FAULT_SIGBUS;
759 	}
760 	mutex_unlock(&ufile->umap_lock);
761 
762 	return ret;
763 }
764 
765 static const struct vm_operations_struct rdma_umap_ops = {
766 	.open = rdma_umap_open,
767 	.close = rdma_umap_close,
768 	.fault = rdma_umap_fault,
769 };
770 
771 void uverbs_user_mmap_disassociate(struct ib_uverbs_file *ufile)
772 {
773 	struct rdma_umap_priv *priv, *next_priv;
774 
775 	mutex_lock(&ufile->disassociation_lock);
776 
777 	while (1) {
778 		struct mm_struct *mm = NULL;
779 
780 		/* Get an arbitrary mm pointer that hasn't been cleaned yet */
781 		mutex_lock(&ufile->umap_lock);
782 		while (!list_empty(&ufile->umaps)) {
783 			int ret;
784 
785 			priv = list_first_entry(&ufile->umaps,
786 						struct rdma_umap_priv, list);
787 			mm = priv->vma->vm_mm;
788 			ret = mmget_not_zero(mm);
789 			if (!ret) {
790 				list_del_init(&priv->list);
791 				if (priv->entry) {
792 					rdma_user_mmap_entry_put(priv->entry);
793 					priv->entry = NULL;
794 				}
795 				mm = NULL;
796 				continue;
797 			}
798 			break;
799 		}
800 		mutex_unlock(&ufile->umap_lock);
801 		if (!mm) {
802 			mutex_unlock(&ufile->disassociation_lock);
803 			return;
804 		}
805 
806 		/*
807 		 * The umap_lock is nested under mmap_lock since it used within
808 		 * the vma_ops callbacks, so we have to clean the list one mm
809 		 * at a time to get the lock ordering right. Typically there
810 		 * will only be one mm, so no big deal.
811 		 */
812 		mmap_read_lock(mm);
813 		mutex_lock(&ufile->umap_lock);
814 		list_for_each_entry_safe (priv, next_priv, &ufile->umaps,
815 					  list) {
816 			struct vm_area_struct *vma = priv->vma;
817 
818 			if (vma->vm_mm != mm)
819 				continue;
820 			list_del_init(&priv->list);
821 
822 			zap_special_vma_range(vma, vma->vm_start,
823 				     vma->vm_end - vma->vm_start);
824 
825 			if (priv->entry) {
826 				rdma_user_mmap_entry_put(priv->entry);
827 				priv->entry = NULL;
828 			}
829 		}
830 		mutex_unlock(&ufile->umap_lock);
831 		mmap_read_unlock(mm);
832 		mmput(mm);
833 	}
834 
835 	mutex_unlock(&ufile->disassociation_lock);
836 }
837 
838 /**
839  * rdma_user_mmap_disassociate() - Revoke mmaps for a device
840  * @device: device to revoke
841  *
842  * This function should be called by drivers that need to disable mmaps for the
843  * device, for instance because it is going to be reset.
844  */
845 void rdma_user_mmap_disassociate(struct ib_device *device)
846 {
847 	struct ib_uverbs_device *uverbs_dev =
848 		ib_get_client_data(device, &uverbs_client);
849 	struct ib_uverbs_file *ufile;
850 
851 	mutex_lock(&uverbs_dev->lists_mutex);
852 	list_for_each_entry(ufile, &uverbs_dev->uverbs_file_list, list) {
853 		if (ufile->ucontext)
854 			uverbs_user_mmap_disassociate(ufile);
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  */
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 	mutex_init(&file->disassociation_lock);
930 
931 	filp->private_data = file;
932 	list_add_tail(&file->list, &dev->uverbs_file_list);
933 	mutex_unlock(&dev->lists_mutex);
934 	srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
935 
936 	setup_ufile_idr_uobject(file);
937 
938 	return stream_open(inode, filp);
939 
940 err_module:
941 	module_put(ib_dev->ops.owner);
942 
943 err:
944 	mutex_unlock(&dev->lists_mutex);
945 	srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
946 	if (refcount_dec_and_test(&dev->refcount))
947 		ib_uverbs_comp_dev(dev);
948 
949 	put_device(&dev->dev);
950 	return ret;
951 }
952 
953 /*
954  * Drop the ucontext off the ufile and completely disconnect it from the
955  * ib_device
956  */
957 static void ufile_destroy_ucontext(struct ib_uverbs_file *ufile,
958 			    enum rdma_remove_reason reason)
959 {
960 	struct ib_ucontext *ucontext = ufile->ucontext;
961 	struct ib_device *ib_dev = ucontext->device;
962 
963 	/*
964 	 * If we are closing the FD then the user mmap VMAs must have
965 	 * already been destroyed as they hold on to the filep, otherwise
966 	 * they need to be zap'd.
967 	 */
968 	if (reason == RDMA_REMOVE_DRIVER_REMOVE) {
969 		uverbs_user_mmap_disassociate(ufile);
970 		if (ib_dev->ops.disassociate_ucontext)
971 			ib_dev->ops.disassociate_ucontext(ucontext);
972 	}
973 
974 	ib_rdmacg_uncharge(&ucontext->cg_obj, ib_dev,
975 			   RDMACG_RESOURCE_HCA_HANDLE);
976 
977 	rdma_restrack_del(&ucontext->res);
978 
979 	ib_dev->ops.dealloc_ucontext(ucontext);
980 	WARN_ON(!xa_empty(&ucontext->mmap_xa));
981 	kfree(ucontext);
982 
983 	ufile->ucontext = NULL;
984 }
985 
986 /*
987  * Destroy the ucontext and every uobject associated with it.
988  *
989  * This is internally locked and can be called in parallel from multiple
990  * contexts.
991  */
992 void uverbs_destroy_ufile_hw(struct ib_uverbs_file *ufile,
993 			     enum rdma_remove_reason reason)
994 {
995 	down_write(&ufile->hw_destroy_rwsem);
996 
997 	/*
998 	 * If a ucontext was never created then we can't have any uobjects to
999 	 * cleanup, nothing to do.
1000 	 */
1001 	if (!ufile->ucontext)
1002 		goto done;
1003 
1004 	while (!list_empty(&ufile->uobjects) &&
1005 	       !__uverbs_cleanup_ufile(ufile, reason)) {
1006 	}
1007 
1008 	if (WARN_ON(!list_empty(&ufile->uobjects)))
1009 		__uverbs_cleanup_ufile(ufile, RDMA_REMOVE_DRIVER_FAILURE);
1010 	ufile_destroy_ucontext(ufile, reason);
1011 
1012 done:
1013 	up_write(&ufile->hw_destroy_rwsem);
1014 }
1015 
1016 static int ib_uverbs_close(struct inode *inode, struct file *filp)
1017 {
1018 	struct ib_uverbs_file *file = filp->private_data;
1019 
1020 	uverbs_destroy_ufile_hw(file, RDMA_REMOVE_CLOSE);
1021 
1022 	mutex_lock(&file->device->lists_mutex);
1023 	list_del_init(&file->list);
1024 	mutex_unlock(&file->device->lists_mutex);
1025 
1026 	kref_put(&file->ref, ib_uverbs_release_file);
1027 
1028 	return 0;
1029 }
1030 
1031 static const struct file_operations uverbs_fops = {
1032 	.owner	 = THIS_MODULE,
1033 	.write	 = ib_uverbs_write,
1034 	.open	 = ib_uverbs_open,
1035 	.release = ib_uverbs_close,
1036 	.unlocked_ioctl = ib_uverbs_ioctl,
1037 	.compat_ioctl = compat_ptr_ioctl,
1038 };
1039 
1040 static const struct file_operations uverbs_mmap_fops = {
1041 	.owner	 = THIS_MODULE,
1042 	.write	 = ib_uverbs_write,
1043 	.mmap    = ib_uverbs_mmap,
1044 	.open	 = ib_uverbs_open,
1045 	.release = ib_uverbs_close,
1046 	.unlocked_ioctl = ib_uverbs_ioctl,
1047 	.compat_ioctl = compat_ptr_ioctl,
1048 };
1049 
1050 static int ib_uverbs_get_nl_info(struct ib_device *ibdev, void *client_data,
1051 				 struct ib_client_nl_info *res)
1052 {
1053 	struct ib_uverbs_device *uverbs_dev = client_data;
1054 	int ret;
1055 
1056 	if (res->port != -1)
1057 		return -EINVAL;
1058 
1059 	res->abi = ibdev->ops.uverbs_abi_ver;
1060 	res->cdev = &uverbs_dev->dev;
1061 
1062 	/*
1063 	 * To support DRIVER_ID binding in userspace some of the driver need
1064 	 * upgrading to expose their PCI dependent revision information
1065 	 * through get_context instead of relying on modalias matching. When
1066 	 * the drivers are fixed they can drop this flag.
1067 	 */
1068 	if (!ibdev->ops.uverbs_no_driver_id_binding) {
1069 		ret = nla_put_u32(res->nl_msg, RDMA_NLDEV_ATTR_UVERBS_DRIVER_ID,
1070 				  ibdev->ops.driver_id);
1071 		if (ret)
1072 			return ret;
1073 	}
1074 	return 0;
1075 }
1076 
1077 static struct ib_client uverbs_client = {
1078 	.name   = "uverbs",
1079 	.no_kverbs_req = true,
1080 	.add    = ib_uverbs_add_one,
1081 	.remove = ib_uverbs_remove_one,
1082 	.get_nl_info = ib_uverbs_get_nl_info,
1083 };
1084 MODULE_ALIAS_RDMA_CLIENT("uverbs");
1085 
1086 static ssize_t ibdev_show(struct device *device, struct device_attribute *attr,
1087 			  char *buf)
1088 {
1089 	struct ib_uverbs_device *dev =
1090 			container_of(device, struct ib_uverbs_device, dev);
1091 	int ret = -ENODEV;
1092 	int srcu_key;
1093 	struct ib_device *ib_dev;
1094 
1095 	srcu_key = srcu_read_lock(&dev->disassociate_srcu);
1096 	ib_dev = srcu_dereference(dev->ib_dev, &dev->disassociate_srcu);
1097 	if (ib_dev)
1098 		ret = sysfs_emit(buf, "%s\n", dev_name(&ib_dev->dev));
1099 	srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
1100 
1101 	return ret;
1102 }
1103 static DEVICE_ATTR_RO(ibdev);
1104 
1105 static ssize_t abi_version_show(struct device *device,
1106 				struct device_attribute *attr, char *buf)
1107 {
1108 	struct ib_uverbs_device *dev =
1109 			container_of(device, struct ib_uverbs_device, dev);
1110 	int ret = -ENODEV;
1111 	int srcu_key;
1112 	struct ib_device *ib_dev;
1113 
1114 	srcu_key = srcu_read_lock(&dev->disassociate_srcu);
1115 	ib_dev = srcu_dereference(dev->ib_dev, &dev->disassociate_srcu);
1116 	if (ib_dev)
1117 		ret = sysfs_emit(buf, "%u\n", ib_dev->ops.uverbs_abi_ver);
1118 	srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
1119 
1120 	return ret;
1121 }
1122 static DEVICE_ATTR_RO(abi_version);
1123 
1124 static struct attribute *ib_dev_attrs[] = {
1125 	&dev_attr_abi_version.attr,
1126 	&dev_attr_ibdev.attr,
1127 	NULL,
1128 };
1129 
1130 static const struct attribute_group dev_attr_group = {
1131 	.attrs = ib_dev_attrs,
1132 };
1133 
1134 static CLASS_ATTR_STRING(abi_version, S_IRUGO,
1135 			 __stringify(IB_USER_VERBS_ABI_VERSION));
1136 
1137 static int ib_uverbs_create_uapi(struct ib_device *device,
1138 				 struct ib_uverbs_device *uverbs_dev)
1139 {
1140 	struct uverbs_api *uapi;
1141 
1142 	uapi = uverbs_alloc_api(device);
1143 	if (IS_ERR(uapi))
1144 		return PTR_ERR(uapi);
1145 
1146 	uverbs_dev->uapi = uapi;
1147 	return 0;
1148 }
1149 
1150 static int ib_uverbs_add_one(struct ib_device *device)
1151 {
1152 	int devnum;
1153 	dev_t base;
1154 	struct ib_uverbs_device *uverbs_dev;
1155 	int ret;
1156 
1157 	if (!device->ops.alloc_ucontext ||
1158 	    device->type == RDMA_DEVICE_TYPE_SMI)
1159 		return -EOPNOTSUPP;
1160 
1161 	uverbs_dev = kzalloc_obj(*uverbs_dev);
1162 	if (!uverbs_dev)
1163 		return -ENOMEM;
1164 
1165 	ret = init_srcu_struct(&uverbs_dev->disassociate_srcu);
1166 	if (ret) {
1167 		kfree(uverbs_dev);
1168 		return -ENOMEM;
1169 	}
1170 
1171 	device_initialize(&uverbs_dev->dev);
1172 	uverbs_dev->dev.class = &uverbs_class;
1173 	uverbs_dev->dev.parent = device->dev.parent;
1174 	uverbs_dev->dev.release = ib_uverbs_release_dev;
1175 	uverbs_dev->groups[0] = &dev_attr_group;
1176 	uverbs_dev->dev.groups = uverbs_dev->groups;
1177 	refcount_set(&uverbs_dev->refcount, 1);
1178 	init_completion(&uverbs_dev->comp);
1179 	uverbs_dev->xrcd_tree = RB_ROOT;
1180 	mutex_init(&uverbs_dev->xrcd_tree_mutex);
1181 	mutex_init(&uverbs_dev->lists_mutex);
1182 	INIT_LIST_HEAD(&uverbs_dev->uverbs_file_list);
1183 	rcu_assign_pointer(uverbs_dev->ib_dev, device);
1184 	uverbs_dev->num_comp_vectors = device->num_comp_vectors;
1185 
1186 	devnum = ida_alloc_max(&uverbs_ida, IB_UVERBS_MAX_DEVICES - 1,
1187 			       GFP_KERNEL);
1188 	if (devnum < 0) {
1189 		ret = -ENOMEM;
1190 		goto err;
1191 	}
1192 	uverbs_dev->devnum = devnum;
1193 	if (devnum >= IB_UVERBS_NUM_FIXED_MINOR)
1194 		base = dynamic_uverbs_dev + devnum - IB_UVERBS_NUM_FIXED_MINOR;
1195 	else
1196 		base = IB_UVERBS_BASE_DEV + devnum;
1197 
1198 	ret = ib_uverbs_create_uapi(device, uverbs_dev);
1199 	if (ret)
1200 		goto err_uapi;
1201 
1202 	uverbs_dev->dev.devt = base;
1203 	dev_set_name(&uverbs_dev->dev, "uverbs%d", uverbs_dev->devnum);
1204 
1205 	cdev_init(&uverbs_dev->cdev,
1206 		  device->ops.mmap ? &uverbs_mmap_fops : &uverbs_fops);
1207 	uverbs_dev->cdev.owner = THIS_MODULE;
1208 
1209 	ret = cdev_device_add(&uverbs_dev->cdev, &uverbs_dev->dev);
1210 	if (ret)
1211 		goto err_uapi;
1212 
1213 	ib_set_client_data(device, &uverbs_client, uverbs_dev);
1214 	return 0;
1215 
1216 err_uapi:
1217 	ida_free(&uverbs_ida, devnum);
1218 err:
1219 	if (refcount_dec_and_test(&uverbs_dev->refcount))
1220 		ib_uverbs_comp_dev(uverbs_dev);
1221 	wait_for_completion(&uverbs_dev->comp);
1222 	put_device(&uverbs_dev->dev);
1223 	return ret;
1224 }
1225 
1226 static void ib_uverbs_free_hw_resources(struct ib_uverbs_device *uverbs_dev,
1227 					struct ib_device *ib_dev)
1228 {
1229 	struct ib_uverbs_file *file;
1230 
1231 	/* Pending running commands to terminate */
1232 	uverbs_disassociate_api_pre(uverbs_dev);
1233 
1234 	mutex_lock(&uverbs_dev->lists_mutex);
1235 	while (!list_empty(&uverbs_dev->uverbs_file_list)) {
1236 		file = list_first_entry(&uverbs_dev->uverbs_file_list,
1237 					struct ib_uverbs_file, list);
1238 		list_del_init(&file->list);
1239 		kref_get(&file->ref);
1240 
1241 		/* We must release the mutex before going ahead and calling
1242 		 * uverbs_cleanup_ufile, as it might end up indirectly calling
1243 		 * uverbs_close, for example due to freeing the resources (e.g
1244 		 * mmput).
1245 		 */
1246 		mutex_unlock(&uverbs_dev->lists_mutex);
1247 
1248 		uverbs_destroy_ufile_hw(file, RDMA_REMOVE_DRIVER_REMOVE);
1249 		kref_put(&file->ref, ib_uverbs_release_file);
1250 
1251 		mutex_lock(&uverbs_dev->lists_mutex);
1252 	}
1253 	mutex_unlock(&uverbs_dev->lists_mutex);
1254 
1255 	uverbs_disassociate_api(uverbs_dev->uapi);
1256 }
1257 
1258 static void ib_uverbs_remove_one(struct ib_device *device, void *client_data)
1259 {
1260 	struct ib_uverbs_device *uverbs_dev = client_data;
1261 	int wait_clients = 1;
1262 
1263 	cdev_device_del(&uverbs_dev->cdev, &uverbs_dev->dev);
1264 	ida_free(&uverbs_ida, uverbs_dev->devnum);
1265 
1266 	if (device->ops.disassociate_ucontext) {
1267 		/* We disassociate HW resources and immediately return.
1268 		 * Userspace will see a EIO errno for all future access.
1269 		 * Upon returning, ib_device may be freed internally and is not
1270 		 * valid any more.
1271 		 * uverbs_device is still available until all clients close
1272 		 * their files, then the uverbs device ref count will be zero
1273 		 * and its resources will be freed.
1274 		 * Note: At this point no more files can be opened since the
1275 		 * cdev was deleted, however active clients can still issue
1276 		 * commands and close their open files.
1277 		 */
1278 		ib_uverbs_free_hw_resources(uverbs_dev, device);
1279 		wait_clients = 0;
1280 	}
1281 
1282 	if (refcount_dec_and_test(&uverbs_dev->refcount))
1283 		ib_uverbs_comp_dev(uverbs_dev);
1284 	if (wait_clients)
1285 		wait_for_completion(&uverbs_dev->comp);
1286 
1287 	put_device(&uverbs_dev->dev);
1288 }
1289 
1290 static int __init ib_uverbs_init(void)
1291 {
1292 	int ret;
1293 
1294 	ret = register_chrdev_region(IB_UVERBS_BASE_DEV,
1295 				     IB_UVERBS_NUM_FIXED_MINOR,
1296 				     "infiniband_verbs");
1297 	if (ret) {
1298 		pr_err("user_verbs: couldn't register device number\n");
1299 		goto out;
1300 	}
1301 
1302 	ret = alloc_chrdev_region(&dynamic_uverbs_dev, 0,
1303 				  IB_UVERBS_NUM_DYNAMIC_MINOR,
1304 				  "infiniband_verbs");
1305 	if (ret) {
1306 		pr_err("couldn't register dynamic device number\n");
1307 		goto out_alloc;
1308 	}
1309 
1310 	ret = class_register(&uverbs_class);
1311 	if (ret) {
1312 		pr_err("user_verbs: couldn't create class infiniband_verbs\n");
1313 		goto out_chrdev;
1314 	}
1315 
1316 	ret = class_create_file(&uverbs_class, &class_attr_abi_version.attr);
1317 	if (ret) {
1318 		pr_err("user_verbs: couldn't create abi_version attribute\n");
1319 		goto out_class;
1320 	}
1321 
1322 	ret = ib_register_client(&uverbs_client);
1323 	if (ret) {
1324 		pr_err("user_verbs: couldn't register client\n");
1325 		goto out_class;
1326 	}
1327 
1328 	return 0;
1329 
1330 out_class:
1331 	class_unregister(&uverbs_class);
1332 
1333 out_chrdev:
1334 	unregister_chrdev_region(dynamic_uverbs_dev,
1335 				 IB_UVERBS_NUM_DYNAMIC_MINOR);
1336 
1337 out_alloc:
1338 	unregister_chrdev_region(IB_UVERBS_BASE_DEV,
1339 				 IB_UVERBS_NUM_FIXED_MINOR);
1340 
1341 out:
1342 	return ret;
1343 }
1344 
1345 static void __exit ib_uverbs_cleanup(void)
1346 {
1347 	ib_unregister_client(&uverbs_client);
1348 	class_unregister(&uverbs_class);
1349 	unregister_chrdev_region(IB_UVERBS_BASE_DEV,
1350 				 IB_UVERBS_NUM_FIXED_MINOR);
1351 	unregister_chrdev_region(dynamic_uverbs_dev,
1352 				 IB_UVERBS_NUM_DYNAMIC_MINOR);
1353 	mmu_notifier_synchronize();
1354 }
1355 
1356 module_init(ib_uverbs_init);
1357 module_exit(ib_uverbs_cleanup);
1358