xref: /linux/drivers/infiniband/core/uverbs_main.c (revision 3a2c4d55e32ad65efebdb6de44eef3bfa08bb49d)
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 	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 
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 	mutex_lock(&file->disassociation_lock);
648 
649 	vma->vm_ops = &rdma_umap_ops;
650 	ret = ucontext->device->ops.mmap(ucontext, vma);
651 
652 	mutex_unlock(&file->disassociation_lock);
653 out:
654 	srcu_read_unlock(&file->device->disassociate_srcu, srcu_key);
655 	return ret;
656 }
657 
658 /*
659  * The VMA has been dup'd, initialize the vm_private_data with a new tracking
660  * struct
661  */
662 static void rdma_umap_open(struct vm_area_struct *vma)
663 {
664 	struct ib_uverbs_file *ufile = vma->vm_file->private_data;
665 	struct rdma_umap_priv *opriv = vma->vm_private_data;
666 	struct rdma_umap_priv *priv;
667 
668 	if (!opriv)
669 		return;
670 
671 	/* We are racing with disassociation */
672 	if (!down_read_trylock(&ufile->hw_destroy_rwsem))
673 		goto out_zap;
674 	mutex_lock(&ufile->disassociation_lock);
675 
676 	/*
677 	 * Disassociation already completed, the VMA should already be zapped.
678 	 */
679 	if (!ufile->ucontext)
680 		goto out_unlock;
681 
682 	priv = kzalloc_obj(*priv);
683 	if (!priv)
684 		goto out_unlock;
685 	rdma_umap_priv_init(priv, vma, opriv->entry);
686 
687 	mutex_unlock(&ufile->disassociation_lock);
688 	up_read(&ufile->hw_destroy_rwsem);
689 	return;
690 
691 out_unlock:
692 	mutex_unlock(&ufile->disassociation_lock);
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 
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  */
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 
772 void uverbs_user_mmap_disassociate(struct ib_uverbs_file *ufile)
773 {
774 	struct rdma_umap_priv *priv, *next_priv;
775 
776 	mutex_lock(&ufile->disassociation_lock);
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 			mutex_unlock(&ufile->disassociation_lock);
804 			return;
805 		}
806 
807 		/*
808 		 * The umap_lock is nested under mmap_lock since it used within
809 		 * the vma_ops callbacks, so we have to clean the list one mm
810 		 * at a time to get the lock ordering right. Typically there
811 		 * will only be one mm, so no big deal.
812 		 */
813 		mmap_read_lock(mm);
814 		mutex_lock(&ufile->umap_lock);
815 		list_for_each_entry_safe (priv, next_priv, &ufile->umaps,
816 					  list) {
817 			struct vm_area_struct *vma = priv->vma;
818 
819 			if (vma->vm_mm != mm)
820 				continue;
821 			list_del_init(&priv->list);
822 
823 			zap_special_vma_range(vma, vma->vm_start,
824 				     vma->vm_end - vma->vm_start);
825 
826 			if (priv->entry) {
827 				rdma_user_mmap_entry_put(priv->entry);
828 				priv->entry = NULL;
829 			}
830 		}
831 		mutex_unlock(&ufile->umap_lock);
832 		mmap_read_unlock(mm);
833 		mmput(mm);
834 	}
835 
836 	mutex_unlock(&ufile->disassociation_lock);
837 }
838 
839 /**
840  * rdma_user_mmap_disassociate() - Revoke mmaps for a device
841  * @device: device to revoke
842  *
843  * This function should be called by drivers that need to disable mmaps for the
844  * device, for instance because it is going to be reset.
845  */
846 void rdma_user_mmap_disassociate(struct ib_device *device)
847 {
848 	struct ib_uverbs_device *uverbs_dev =
849 		ib_get_client_data(device, &uverbs_client);
850 	struct ib_uverbs_file *ufile;
851 
852 	mutex_lock(&uverbs_dev->lists_mutex);
853 	list_for_each_entry(ufile, &uverbs_dev->uverbs_file_list, list) {
854 		if (ufile->ucontext)
855 			uverbs_user_mmap_disassociate(ufile);
856 	}
857 	mutex_unlock(&uverbs_dev->lists_mutex);
858 }
859 EXPORT_SYMBOL(rdma_user_mmap_disassociate);
860 
861 /*
862  * ib_uverbs_open() does not need the BKL:
863  *
864  *  - the ib_uverbs_device structures are properly reference counted and
865  *    everything else is purely local to the file being created, so
866  *    races against other open calls are not a problem;
867  *  - there is no ioctl method to race against;
868  *  - the open method will either immediately run -ENXIO, or all
869  *    required initialization will be done.
870  */
871 static int ib_uverbs_open(struct inode *inode, struct file *filp)
872 {
873 	struct ib_uverbs_device *dev;
874 	struct ib_uverbs_file *file;
875 	struct ib_device *ib_dev;
876 	int ret;
877 	int module_dependent;
878 	int srcu_key;
879 
880 	dev = container_of(inode->i_cdev, struct ib_uverbs_device, cdev);
881 	if (!refcount_inc_not_zero(&dev->refcount))
882 		return -ENXIO;
883 
884 	get_device(&dev->dev);
885 	srcu_key = srcu_read_lock(&dev->disassociate_srcu);
886 	mutex_lock(&dev->lists_mutex);
887 	ib_dev = srcu_dereference(dev->ib_dev,
888 				  &dev->disassociate_srcu);
889 	if (!ib_dev) {
890 		ret = -EIO;
891 		goto err;
892 	}
893 
894 	if (!rdma_dev_access_netns(ib_dev, current->nsproxy->net_ns)) {
895 		ret = -EPERM;
896 		goto err;
897 	}
898 
899 	/* In case IB device supports disassociate ucontext, there is no hard
900 	 * dependency between uverbs device and its low level device.
901 	 */
902 	module_dependent = !(ib_dev->ops.disassociate_ucontext);
903 
904 	if (module_dependent) {
905 		if (!try_module_get(ib_dev->ops.owner)) {
906 			ret = -ENODEV;
907 			goto err;
908 		}
909 	}
910 
911 	file = kzalloc_obj(*file);
912 	if (!file) {
913 		ret = -ENOMEM;
914 		if (module_dependent)
915 			goto err_module;
916 
917 		goto err;
918 	}
919 
920 	file->device	 = dev;
921 	kref_init(&file->ref);
922 	mutex_init(&file->ucontext_lock);
923 
924 	spin_lock_init(&file->uobjects_lock);
925 	INIT_LIST_HEAD(&file->uobjects);
926 	init_rwsem(&file->hw_destroy_rwsem);
927 	mutex_init(&file->umap_lock);
928 	INIT_LIST_HEAD(&file->umaps);
929 
930 	mutex_init(&file->disassociation_lock);
931 
932 	filp->private_data = file;
933 	list_add_tail(&file->list, &dev->uverbs_file_list);
934 	mutex_unlock(&dev->lists_mutex);
935 	srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
936 
937 	setup_ufile_idr_uobject(file);
938 
939 	return stream_open(inode, filp);
940 
941 err_module:
942 	module_put(ib_dev->ops.owner);
943 
944 err:
945 	mutex_unlock(&dev->lists_mutex);
946 	srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
947 	if (refcount_dec_and_test(&dev->refcount))
948 		ib_uverbs_comp_dev(dev);
949 
950 	put_device(&dev->dev);
951 	return ret;
952 }
953 
954 /*
955  * Drop the ucontext off the ufile and completely disconnect it from the
956  * ib_device
957  */
958 static void ufile_destroy_ucontext(struct ib_uverbs_file *ufile,
959 			    enum rdma_remove_reason reason)
960 {
961 	struct ib_ucontext *ucontext = ufile->ucontext;
962 	struct ib_device *ib_dev = ucontext->device;
963 
964 	/*
965 	 * If we are closing the FD then the user mmap VMAs must have
966 	 * already been destroyed as they hold on to the filep, otherwise
967 	 * they need to be zap'd.
968 	 */
969 	if (reason == RDMA_REMOVE_DRIVER_REMOVE) {
970 		uverbs_user_mmap_disassociate(ufile);
971 		if (ib_dev->ops.disassociate_ucontext)
972 			ib_dev->ops.disassociate_ucontext(ucontext);
973 	}
974 
975 	ib_rdmacg_uncharge(&ucontext->cg_obj, ib_dev,
976 			   RDMACG_RESOURCE_HCA_HANDLE);
977 
978 	rdma_restrack_del(&ucontext->res);
979 
980 	ib_dev->ops.dealloc_ucontext(ucontext);
981 	WARN_ON(!xa_empty(&ucontext->mmap_xa));
982 	kfree(ucontext);
983 
984 	ufile->ucontext = NULL;
985 }
986 
987 /*
988  * Destroy the ucontext and every uobject associated with it.
989  *
990  * This is internally locked and can be called in parallel from multiple
991  * contexts.
992  */
993 void uverbs_destroy_ufile_hw(struct ib_uverbs_file *ufile,
994 			     enum rdma_remove_reason reason)
995 {
996 	down_write(&ufile->hw_destroy_rwsem);
997 
998 	/*
999 	 * If a ucontext was never created then we can't have any uobjects to
1000 	 * cleanup, nothing to do.
1001 	 */
1002 	if (!ufile->ucontext)
1003 		goto done;
1004 
1005 	while (!list_empty(&ufile->uobjects) &&
1006 	       !__uverbs_cleanup_ufile(ufile, reason)) {
1007 	}
1008 
1009 	if (WARN_ON(!list_empty(&ufile->uobjects)))
1010 		__uverbs_cleanup_ufile(ufile, RDMA_REMOVE_DRIVER_FAILURE);
1011 	ufile_destroy_ucontext(ufile, reason);
1012 
1013 done:
1014 	up_write(&ufile->hw_destroy_rwsem);
1015 }
1016 
1017 static int ib_uverbs_close(struct inode *inode, struct file *filp)
1018 {
1019 	struct ib_uverbs_file *file = filp->private_data;
1020 
1021 	uverbs_destroy_ufile_hw(file, RDMA_REMOVE_CLOSE);
1022 
1023 	mutex_lock(&file->device->lists_mutex);
1024 	list_del_init(&file->list);
1025 	mutex_unlock(&file->device->lists_mutex);
1026 
1027 	kref_put(&file->ref, ib_uverbs_release_file);
1028 
1029 	return 0;
1030 }
1031 
1032 static const struct file_operations uverbs_fops = {
1033 	.owner	 = THIS_MODULE,
1034 	.write	 = ib_uverbs_write,
1035 	.open	 = ib_uverbs_open,
1036 	.release = ib_uverbs_close,
1037 	.unlocked_ioctl = ib_uverbs_ioctl,
1038 	.compat_ioctl = compat_ptr_ioctl,
1039 };
1040 
1041 static const struct file_operations uverbs_mmap_fops = {
1042 	.owner	 = THIS_MODULE,
1043 	.write	 = ib_uverbs_write,
1044 	.mmap    = ib_uverbs_mmap,
1045 	.open	 = ib_uverbs_open,
1046 	.release = ib_uverbs_close,
1047 	.unlocked_ioctl = ib_uverbs_ioctl,
1048 	.compat_ioctl = compat_ptr_ioctl,
1049 };
1050 
1051 static int ib_uverbs_get_nl_info(struct ib_device *ibdev, void *client_data,
1052 				 struct ib_client_nl_info *res)
1053 {
1054 	struct ib_uverbs_device *uverbs_dev = client_data;
1055 	int ret;
1056 
1057 	if (res->port != -1)
1058 		return -EINVAL;
1059 
1060 	res->abi = ibdev->ops.uverbs_abi_ver;
1061 	res->cdev = &uverbs_dev->dev;
1062 
1063 	/*
1064 	 * To support DRIVER_ID binding in userspace some of the driver need
1065 	 * upgrading to expose their PCI dependent revision information
1066 	 * through get_context instead of relying on modalias matching. When
1067 	 * the drivers are fixed they can drop this flag.
1068 	 */
1069 	if (!ibdev->ops.uverbs_no_driver_id_binding) {
1070 		ret = nla_put_u32(res->nl_msg, RDMA_NLDEV_ATTR_UVERBS_DRIVER_ID,
1071 				  ibdev->ops.driver_id);
1072 		if (ret)
1073 			return ret;
1074 	}
1075 	return 0;
1076 }
1077 
1078 static struct ib_client uverbs_client = {
1079 	.name   = "uverbs",
1080 	.no_kverbs_req = true,
1081 	.add    = ib_uverbs_add_one,
1082 	.remove = ib_uverbs_remove_one,
1083 	.get_nl_info = ib_uverbs_get_nl_info,
1084 };
1085 MODULE_ALIAS_RDMA_CLIENT("uverbs");
1086 
1087 static ssize_t ibdev_show(struct device *device, struct device_attribute *attr,
1088 			  char *buf)
1089 {
1090 	struct ib_uverbs_device *dev =
1091 			container_of(device, struct ib_uverbs_device, dev);
1092 	int ret = -ENODEV;
1093 	int srcu_key;
1094 	struct ib_device *ib_dev;
1095 
1096 	srcu_key = srcu_read_lock(&dev->disassociate_srcu);
1097 	ib_dev = srcu_dereference(dev->ib_dev, &dev->disassociate_srcu);
1098 	if (ib_dev)
1099 		ret = sysfs_emit(buf, "%s\n", dev_name(&ib_dev->dev));
1100 	srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
1101 
1102 	return ret;
1103 }
1104 static DEVICE_ATTR_RO(ibdev);
1105 
1106 static ssize_t abi_version_show(struct device *device,
1107 				struct device_attribute *attr, char *buf)
1108 {
1109 	struct ib_uverbs_device *dev =
1110 			container_of(device, struct ib_uverbs_device, dev);
1111 	int ret = -ENODEV;
1112 	int srcu_key;
1113 	struct ib_device *ib_dev;
1114 
1115 	srcu_key = srcu_read_lock(&dev->disassociate_srcu);
1116 	ib_dev = srcu_dereference(dev->ib_dev, &dev->disassociate_srcu);
1117 	if (ib_dev)
1118 		ret = sysfs_emit(buf, "%u\n", ib_dev->ops.uverbs_abi_ver);
1119 	srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
1120 
1121 	return ret;
1122 }
1123 static DEVICE_ATTR_RO(abi_version);
1124 
1125 static struct attribute *ib_dev_attrs[] = {
1126 	&dev_attr_abi_version.attr,
1127 	&dev_attr_ibdev.attr,
1128 	NULL,
1129 };
1130 
1131 static const struct attribute_group dev_attr_group = {
1132 	.attrs = ib_dev_attrs,
1133 };
1134 
1135 static CLASS_ATTR_STRING(abi_version, S_IRUGO,
1136 			 __stringify(IB_USER_VERBS_ABI_VERSION));
1137 
1138 static int ib_uverbs_create_uapi(struct ib_device *device,
1139 				 struct ib_uverbs_device *uverbs_dev)
1140 {
1141 	struct uverbs_api *uapi;
1142 
1143 	uapi = uverbs_alloc_api(device);
1144 	if (IS_ERR(uapi))
1145 		return PTR_ERR(uapi);
1146 
1147 	uverbs_dev->uapi = uapi;
1148 	return 0;
1149 }
1150 
1151 static int ib_uverbs_add_one(struct ib_device *device)
1152 {
1153 	int devnum;
1154 	dev_t base;
1155 	struct ib_uverbs_device *uverbs_dev;
1156 	int ret;
1157 
1158 	if (!device->ops.alloc_ucontext ||
1159 	    device->type == RDMA_DEVICE_TYPE_SMI)
1160 		return -EOPNOTSUPP;
1161 
1162 	uverbs_dev = kzalloc_obj(*uverbs_dev);
1163 	if (!uverbs_dev)
1164 		return -ENOMEM;
1165 
1166 	ret = init_srcu_struct(&uverbs_dev->disassociate_srcu);
1167 	if (ret) {
1168 		kfree(uverbs_dev);
1169 		return -ENOMEM;
1170 	}
1171 
1172 	device_initialize(&uverbs_dev->dev);
1173 	uverbs_dev->dev.class = &uverbs_class;
1174 	uverbs_dev->dev.parent = device->dev.parent;
1175 	uverbs_dev->dev.release = ib_uverbs_release_dev;
1176 	uverbs_dev->groups[0] = &dev_attr_group;
1177 	uverbs_dev->dev.groups = uverbs_dev->groups;
1178 	refcount_set(&uverbs_dev->refcount, 1);
1179 	init_completion(&uverbs_dev->comp);
1180 	uverbs_dev->xrcd_tree = RB_ROOT;
1181 	mutex_init(&uverbs_dev->xrcd_tree_mutex);
1182 	mutex_init(&uverbs_dev->lists_mutex);
1183 	INIT_LIST_HEAD(&uverbs_dev->uverbs_file_list);
1184 	rcu_assign_pointer(uverbs_dev->ib_dev, device);
1185 	uverbs_dev->num_comp_vectors = device->num_comp_vectors;
1186 
1187 	devnum = ida_alloc_max(&uverbs_ida, IB_UVERBS_MAX_DEVICES - 1,
1188 			       GFP_KERNEL);
1189 	if (devnum < 0) {
1190 		ret = -ENOMEM;
1191 		goto err;
1192 	}
1193 	uverbs_dev->devnum = devnum;
1194 	if (devnum >= IB_UVERBS_NUM_FIXED_MINOR)
1195 		base = dynamic_uverbs_dev + devnum - IB_UVERBS_NUM_FIXED_MINOR;
1196 	else
1197 		base = IB_UVERBS_BASE_DEV + devnum;
1198 
1199 	ret = ib_uverbs_create_uapi(device, uverbs_dev);
1200 	if (ret)
1201 		goto err_uapi;
1202 
1203 	uverbs_dev->dev.devt = base;
1204 	dev_set_name(&uverbs_dev->dev, "uverbs%d", uverbs_dev->devnum);
1205 
1206 	cdev_init(&uverbs_dev->cdev,
1207 		  device->ops.mmap ? &uverbs_mmap_fops : &uverbs_fops);
1208 	uverbs_dev->cdev.owner = THIS_MODULE;
1209 
1210 	ret = cdev_device_add(&uverbs_dev->cdev, &uverbs_dev->dev);
1211 	if (ret)
1212 		goto err_uapi;
1213 
1214 	ib_set_client_data(device, &uverbs_client, uverbs_dev);
1215 	return 0;
1216 
1217 err_uapi:
1218 	ida_free(&uverbs_ida, devnum);
1219 err:
1220 	if (refcount_dec_and_test(&uverbs_dev->refcount))
1221 		ib_uverbs_comp_dev(uverbs_dev);
1222 	wait_for_completion(&uverbs_dev->comp);
1223 	put_device(&uverbs_dev->dev);
1224 	return ret;
1225 }
1226 
1227 static void ib_uverbs_free_hw_resources(struct ib_uverbs_device *uverbs_dev,
1228 					struct ib_device *ib_dev)
1229 {
1230 	struct ib_uverbs_file *file;
1231 
1232 	/* Pending running commands to terminate */
1233 	uverbs_disassociate_api_pre(uverbs_dev);
1234 
1235 	mutex_lock(&uverbs_dev->lists_mutex);
1236 	while (!list_empty(&uverbs_dev->uverbs_file_list)) {
1237 		file = list_first_entry(&uverbs_dev->uverbs_file_list,
1238 					struct ib_uverbs_file, list);
1239 		list_del_init(&file->list);
1240 		kref_get(&file->ref);
1241 
1242 		/* We must release the mutex before going ahead and calling
1243 		 * uverbs_cleanup_ufile, as it might end up indirectly calling
1244 		 * uverbs_close, for example due to freeing the resources (e.g
1245 		 * mmput).
1246 		 */
1247 		mutex_unlock(&uverbs_dev->lists_mutex);
1248 
1249 		uverbs_destroy_ufile_hw(file, RDMA_REMOVE_DRIVER_REMOVE);
1250 		kref_put(&file->ref, ib_uverbs_release_file);
1251 
1252 		mutex_lock(&uverbs_dev->lists_mutex);
1253 	}
1254 	mutex_unlock(&uverbs_dev->lists_mutex);
1255 
1256 	uverbs_disassociate_api(uverbs_dev->uapi);
1257 }
1258 
1259 static void ib_uverbs_remove_one(struct ib_device *device, void *client_data)
1260 {
1261 	struct ib_uverbs_device *uverbs_dev = client_data;
1262 	int wait_clients = 1;
1263 
1264 	cdev_device_del(&uverbs_dev->cdev, &uverbs_dev->dev);
1265 	ida_free(&uverbs_ida, uverbs_dev->devnum);
1266 
1267 	if (device->ops.disassociate_ucontext) {
1268 		/* We disassociate HW resources and immediately return.
1269 		 * Userspace will see a EIO errno for all future access.
1270 		 * Upon returning, ib_device may be freed internally and is not
1271 		 * valid any more.
1272 		 * uverbs_device is still available until all clients close
1273 		 * their files, then the uverbs device ref count will be zero
1274 		 * and its resources will be freed.
1275 		 * Note: At this point no more files can be opened since the
1276 		 * cdev was deleted, however active clients can still issue
1277 		 * commands and close their open files.
1278 		 */
1279 		ib_uverbs_free_hw_resources(uverbs_dev, device);
1280 		wait_clients = 0;
1281 	}
1282 
1283 	if (refcount_dec_and_test(&uverbs_dev->refcount))
1284 		ib_uverbs_comp_dev(uverbs_dev);
1285 	if (wait_clients)
1286 		wait_for_completion(&uverbs_dev->comp);
1287 
1288 	put_device(&uverbs_dev->dev);
1289 }
1290 
1291 static int __init ib_uverbs_init(void)
1292 {
1293 	int ret;
1294 
1295 	ret = register_chrdev_region(IB_UVERBS_BASE_DEV,
1296 				     IB_UVERBS_NUM_FIXED_MINOR,
1297 				     "infiniband_verbs");
1298 	if (ret) {
1299 		pr_err("user_verbs: couldn't register device number\n");
1300 		goto out;
1301 	}
1302 
1303 	ret = alloc_chrdev_region(&dynamic_uverbs_dev, 0,
1304 				  IB_UVERBS_NUM_DYNAMIC_MINOR,
1305 				  "infiniband_verbs");
1306 	if (ret) {
1307 		pr_err("couldn't register dynamic device number\n");
1308 		goto out_alloc;
1309 	}
1310 
1311 	ret = class_register(&uverbs_class);
1312 	if (ret) {
1313 		pr_err("user_verbs: couldn't create class infiniband_verbs\n");
1314 		goto out_chrdev;
1315 	}
1316 
1317 	ret = class_create_file(&uverbs_class, &class_attr_abi_version.attr);
1318 	if (ret) {
1319 		pr_err("user_verbs: couldn't create abi_version attribute\n");
1320 		goto out_class;
1321 	}
1322 
1323 	ret = ib_register_client(&uverbs_client);
1324 	if (ret) {
1325 		pr_err("user_verbs: couldn't register client\n");
1326 		goto out_class;
1327 	}
1328 
1329 	return 0;
1330 
1331 out_class:
1332 	class_unregister(&uverbs_class);
1333 
1334 out_chrdev:
1335 	unregister_chrdev_region(dynamic_uverbs_dev,
1336 				 IB_UVERBS_NUM_DYNAMIC_MINOR);
1337 
1338 out_alloc:
1339 	unregister_chrdev_region(IB_UVERBS_BASE_DEV,
1340 				 IB_UVERBS_NUM_FIXED_MINOR);
1341 
1342 out:
1343 	return ret;
1344 }
1345 
1346 static void __exit ib_uverbs_cleanup(void)
1347 {
1348 	ib_unregister_client(&uverbs_client);
1349 	class_unregister(&uverbs_class);
1350 	unregister_chrdev_region(IB_UVERBS_BASE_DEV,
1351 				 IB_UVERBS_NUM_FIXED_MINOR);
1352 	unregister_chrdev_region(dynamic_uverbs_dev,
1353 				 IB_UVERBS_NUM_DYNAMIC_MINOR);
1354 	mmu_notifier_synchronize();
1355 }
1356 
1357 module_init(ib_uverbs_init);
1358 module_exit(ib_uverbs_cleanup);
1359