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