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 	file->ev_queue.is_closed = 1;
444 	spin_unlock_irq(&file->ev_queue.lock);
445 
446 	uverbs_close_fd(filp);
447 
448 	return 0;
449 }
450 
451 const struct file_operations uverbs_event_fops = {
452 	.owner	 = THIS_MODULE,
453 	.read	 = ib_uverbs_comp_event_read,
454 	.poll    = ib_uverbs_comp_event_poll,
455 	.release = ib_uverbs_comp_event_close,
456 	.fasync  = ib_uverbs_comp_event_fasync,
457 	.llseek	 = no_llseek,
458 };
459 
460 static const struct file_operations uverbs_async_event_fops = {
461 	.owner	 = THIS_MODULE,
462 	.read	 = ib_uverbs_async_event_read,
463 	.poll    = ib_uverbs_async_event_poll,
464 	.release = ib_uverbs_async_event_close,
465 	.fasync  = ib_uverbs_async_event_fasync,
466 	.llseek	 = no_llseek,
467 };
468 
469 void ib_uverbs_comp_handler(struct ib_cq *cq, void *cq_context)
470 {
471 	struct ib_uverbs_event_queue   *ev_queue = cq_context;
472 	struct ib_ucq_object	       *uobj;
473 	struct ib_uverbs_event	       *entry;
474 	unsigned long			flags;
475 
476 	if (!ev_queue)
477 		return;
478 
479 	spin_lock_irqsave(&ev_queue->lock, flags);
480 	if (ev_queue->is_closed) {
481 		spin_unlock_irqrestore(&ev_queue->lock, flags);
482 		return;
483 	}
484 
485 	entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
486 	if (!entry) {
487 		spin_unlock_irqrestore(&ev_queue->lock, flags);
488 		return;
489 	}
490 
491 	uobj = container_of(cq->uobject, struct ib_ucq_object, uobject);
492 
493 	entry->desc.comp.cq_handle = cq->uobject->user_handle;
494 	entry->counter		   = &uobj->comp_events_reported;
495 
496 	list_add_tail(&entry->list, &ev_queue->event_list);
497 	list_add_tail(&entry->obj_list, &uobj->comp_list);
498 	spin_unlock_irqrestore(&ev_queue->lock, flags);
499 
500 	wake_up_interruptible(&ev_queue->poll_wait);
501 	kill_fasync(&ev_queue->async_queue, SIGIO, POLL_IN);
502 }
503 
504 static void ib_uverbs_async_handler(struct ib_uverbs_file *file,
505 				    __u64 element, __u64 event,
506 				    struct list_head *obj_list,
507 				    u32 *counter)
508 {
509 	struct ib_uverbs_event *entry;
510 	unsigned long flags;
511 
512 	spin_lock_irqsave(&file->async_file->ev_queue.lock, flags);
513 	if (file->async_file->ev_queue.is_closed) {
514 		spin_unlock_irqrestore(&file->async_file->ev_queue.lock, flags);
515 		return;
516 	}
517 
518 	entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
519 	if (!entry) {
520 		spin_unlock_irqrestore(&file->async_file->ev_queue.lock, flags);
521 		return;
522 	}
523 
524 	entry->desc.async.element    = element;
525 	entry->desc.async.event_type = event;
526 	entry->desc.async.reserved   = 0;
527 	entry->counter               = counter;
528 
529 	list_add_tail(&entry->list, &file->async_file->ev_queue.event_list);
530 	if (obj_list)
531 		list_add_tail(&entry->obj_list, obj_list);
532 	spin_unlock_irqrestore(&file->async_file->ev_queue.lock, flags);
533 
534 	wake_up_interruptible(&file->async_file->ev_queue.poll_wait);
535 	kill_fasync(&file->async_file->ev_queue.async_queue, SIGIO, POLL_IN);
536 }
537 
538 void ib_uverbs_cq_event_handler(struct ib_event *event, void *context_ptr)
539 {
540 	struct ib_ucq_object *uobj = container_of(event->element.cq->uobject,
541 						  struct ib_ucq_object, uobject);
542 
543 	ib_uverbs_async_handler(uobj->uobject.ufile, uobj->uobject.user_handle,
544 				event->event, &uobj->async_list,
545 				&uobj->async_events_reported);
546 }
547 
548 void ib_uverbs_qp_event_handler(struct ib_event *event, void *context_ptr)
549 {
550 	struct ib_uevent_object *uobj;
551 
552 	/* for XRC target qp's, check that qp is live */
553 	if (!event->element.qp->uobject)
554 		return;
555 
556 	uobj = container_of(event->element.qp->uobject,
557 			    struct ib_uevent_object, uobject);
558 
559 	ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle,
560 				event->event, &uobj->event_list,
561 				&uobj->events_reported);
562 }
563 
564 void ib_uverbs_wq_event_handler(struct ib_event *event, void *context_ptr)
565 {
566 	struct ib_uevent_object *uobj = container_of(event->element.wq->uobject,
567 						  struct ib_uevent_object, uobject);
568 
569 	ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle,
570 				event->event, &uobj->event_list,
571 				&uobj->events_reported);
572 }
573 
574 void ib_uverbs_srq_event_handler(struct ib_event *event, void *context_ptr)
575 {
576 	struct ib_uevent_object *uobj;
577 
578 	uobj = container_of(event->element.srq->uobject,
579 			    struct ib_uevent_object, uobject);
580 
581 	ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle,
582 				event->event, &uobj->event_list,
583 				&uobj->events_reported);
584 }
585 
586 void ib_uverbs_event_handler(struct ib_event_handler *handler,
587 			     struct ib_event *event)
588 {
589 	struct ib_uverbs_file *file =
590 		container_of(handler, struct ib_uverbs_file, event_handler);
591 
592 	ib_uverbs_async_handler(file, event->element.port_num, event->event,
593 				NULL, NULL);
594 }
595 
596 void ib_uverbs_free_async_event_file(struct ib_uverbs_file *file)
597 {
598 	kref_put(&file->async_file->ref, ib_uverbs_release_async_event_file);
599 	file->async_file = NULL;
600 }
601 
602 void ib_uverbs_init_event_queue(struct ib_uverbs_event_queue *ev_queue)
603 {
604 	spin_lock_init(&ev_queue->lock);
605 	INIT_LIST_HEAD(&ev_queue->event_list);
606 	init_waitqueue_head(&ev_queue->poll_wait);
607 	ev_queue->is_closed   = 0;
608 	ev_queue->async_queue = NULL;
609 }
610 
611 struct file *ib_uverbs_alloc_async_event_file(struct ib_uverbs_file *uverbs_file,
612 					      struct ib_device	*ib_dev)
613 {
614 	struct ib_uverbs_async_event_file *ev_file;
615 	struct file *filp;
616 
617 	ev_file = kzalloc(sizeof(*ev_file), GFP_KERNEL);
618 	if (!ev_file)
619 		return ERR_PTR(-ENOMEM);
620 
621 	ib_uverbs_init_event_queue(&ev_file->ev_queue);
622 	ev_file->uverbs_file = uverbs_file;
623 	kref_get(&ev_file->uverbs_file->ref);
624 	kref_init(&ev_file->ref);
625 	filp = anon_inode_getfile("[infinibandevent]", &uverbs_async_event_fops,
626 				  ev_file, O_RDONLY);
627 	if (IS_ERR(filp))
628 		goto err_put_refs;
629 
630 	mutex_lock(&uverbs_file->device->lists_mutex);
631 	list_add_tail(&ev_file->list,
632 		      &uverbs_file->device->uverbs_events_file_list);
633 	mutex_unlock(&uverbs_file->device->lists_mutex);
634 
635 	WARN_ON(uverbs_file->async_file);
636 	uverbs_file->async_file = ev_file;
637 	kref_get(&uverbs_file->async_file->ref);
638 	INIT_IB_EVENT_HANDLER(&uverbs_file->event_handler,
639 			      ib_dev,
640 			      ib_uverbs_event_handler);
641 	ib_register_event_handler(&uverbs_file->event_handler);
642 	/* At that point async file stuff was fully set */
643 
644 	return filp;
645 
646 err_put_refs:
647 	kref_put(&ev_file->uverbs_file->ref, ib_uverbs_release_file);
648 	kref_put(&ev_file->ref, ib_uverbs_release_async_event_file);
649 	return filp;
650 }
651 
652 static bool verify_command_mask(struct ib_uverbs_file *ufile, u32 command,
653 				bool extended)
654 {
655 	if (!extended)
656 		return ufile->uverbs_cmd_mask & BIT_ULL(command);
657 
658 	return ufile->uverbs_ex_cmd_mask & BIT_ULL(command);
659 }
660 
661 static bool verify_command_idx(u32 command, bool extended)
662 {
663 	if (extended)
664 		return command < ARRAY_SIZE(uverbs_ex_cmd_table) &&
665 		       uverbs_ex_cmd_table[command];
666 
667 	return command < ARRAY_SIZE(uverbs_cmd_table) &&
668 	       uverbs_cmd_table[command];
669 }
670 
671 static ssize_t process_hdr(struct ib_uverbs_cmd_hdr *hdr,
672 			   u32 *command, bool *extended)
673 {
674 	if (hdr->command & ~(u32)(IB_USER_VERBS_CMD_FLAG_EXTENDED |
675 				   IB_USER_VERBS_CMD_COMMAND_MASK))
676 		return -EINVAL;
677 
678 	*command = hdr->command & IB_USER_VERBS_CMD_COMMAND_MASK;
679 	*extended = hdr->command & IB_USER_VERBS_CMD_FLAG_EXTENDED;
680 
681 	if (!verify_command_idx(*command, *extended))
682 		return -EOPNOTSUPP;
683 
684 	return 0;
685 }
686 
687 static ssize_t verify_hdr(struct ib_uverbs_cmd_hdr *hdr,
688 			  struct ib_uverbs_ex_cmd_hdr *ex_hdr,
689 			  size_t count, bool extended)
690 {
691 	if (extended) {
692 		count -= sizeof(*hdr) + sizeof(*ex_hdr);
693 
694 		if ((hdr->in_words + ex_hdr->provider_in_words) * 8 != count)
695 			return -EINVAL;
696 
697 		if (ex_hdr->cmd_hdr_reserved)
698 			return -EINVAL;
699 
700 		if (ex_hdr->response) {
701 			if (!hdr->out_words && !ex_hdr->provider_out_words)
702 				return -EINVAL;
703 
704 			if (!access_ok(VERIFY_WRITE,
705 				       u64_to_user_ptr(ex_hdr->response),
706 				       (hdr->out_words + ex_hdr->provider_out_words) * 8))
707 				return -EFAULT;
708 		} else {
709 			if (hdr->out_words || ex_hdr->provider_out_words)
710 				return -EINVAL;
711 		}
712 
713 		return 0;
714 	}
715 
716 	/* not extended command */
717 	if (hdr->in_words * 4 != count)
718 		return -EINVAL;
719 
720 	return 0;
721 }
722 
723 static ssize_t ib_uverbs_write(struct file *filp, const char __user *buf,
724 			     size_t count, loff_t *pos)
725 {
726 	struct ib_uverbs_file *file = filp->private_data;
727 	struct ib_uverbs_ex_cmd_hdr ex_hdr;
728 	struct ib_uverbs_cmd_hdr hdr;
729 	bool extended;
730 	int srcu_key;
731 	u32 command;
732 	ssize_t ret;
733 
734 	if (!ib_safe_file_access(filp)) {
735 		pr_err_once("uverbs_write: process %d (%s) changed security contexts after opening file descriptor, this is not allowed.\n",
736 			    task_tgid_vnr(current), current->comm);
737 		return -EACCES;
738 	}
739 
740 	if (count < sizeof(hdr))
741 		return -EINVAL;
742 
743 	if (copy_from_user(&hdr, buf, sizeof(hdr)))
744 		return -EFAULT;
745 
746 	ret = process_hdr(&hdr, &command, &extended);
747 	if (ret)
748 		return ret;
749 
750 	if (extended) {
751 		if (count < (sizeof(hdr) + sizeof(ex_hdr)))
752 			return -EINVAL;
753 		if (copy_from_user(&ex_hdr, buf + sizeof(hdr), sizeof(ex_hdr)))
754 			return -EFAULT;
755 	}
756 
757 	ret = verify_hdr(&hdr, &ex_hdr, count, extended);
758 	if (ret)
759 		return ret;
760 
761 	srcu_key = srcu_read_lock(&file->device->disassociate_srcu);
762 
763 	if (!verify_command_mask(file, command, extended)) {
764 		ret = -EOPNOTSUPP;
765 		goto out;
766 	}
767 
768 	buf += sizeof(hdr);
769 
770 	if (!extended) {
771 		ret = uverbs_cmd_table[command](file, buf,
772 						hdr.in_words * 4,
773 						hdr.out_words * 4);
774 	} else {
775 		struct ib_udata ucore;
776 		struct ib_udata uhw;
777 
778 		buf += sizeof(ex_hdr);
779 
780 		ib_uverbs_init_udata_buf_or_null(&ucore, buf,
781 					u64_to_user_ptr(ex_hdr.response),
782 					hdr.in_words * 8, hdr.out_words * 8);
783 
784 		ib_uverbs_init_udata_buf_or_null(&uhw,
785 					buf + ucore.inlen,
786 					u64_to_user_ptr(ex_hdr.response) + ucore.outlen,
787 					ex_hdr.provider_in_words * 8,
788 					ex_hdr.provider_out_words * 8);
789 
790 		ret = uverbs_ex_cmd_table[command](file, &ucore, &uhw);
791 		ret = (ret) ? : count;
792 	}
793 
794 out:
795 	srcu_read_unlock(&file->device->disassociate_srcu, srcu_key);
796 	return ret;
797 }
798 
799 static int ib_uverbs_mmap(struct file *filp, struct vm_area_struct *vma)
800 {
801 	struct ib_uverbs_file *file = filp->private_data;
802 	struct ib_ucontext *ucontext;
803 	int ret = 0;
804 	int srcu_key;
805 
806 	srcu_key = srcu_read_lock(&file->device->disassociate_srcu);
807 	ucontext = ib_uverbs_get_ucontext(file);
808 	if (IS_ERR(ucontext)) {
809 		ret = PTR_ERR(ucontext);
810 		goto out;
811 	}
812 
813 	ret = ucontext->device->mmap(ucontext, vma);
814 out:
815 	srcu_read_unlock(&file->device->disassociate_srcu, srcu_key);
816 	return ret;
817 }
818 
819 /*
820  * ib_uverbs_open() does not need the BKL:
821  *
822  *  - the ib_uverbs_device structures are properly reference counted and
823  *    everything else is purely local to the file being created, so
824  *    races against other open calls are not a problem;
825  *  - there is no ioctl method to race against;
826  *  - the open method will either immediately run -ENXIO, or all
827  *    required initialization will be done.
828  */
829 static int ib_uverbs_open(struct inode *inode, struct file *filp)
830 {
831 	struct ib_uverbs_device *dev;
832 	struct ib_uverbs_file *file;
833 	struct ib_device *ib_dev;
834 	int ret;
835 	int module_dependent;
836 	int srcu_key;
837 
838 	dev = container_of(inode->i_cdev, struct ib_uverbs_device, cdev);
839 	if (!atomic_inc_not_zero(&dev->refcount))
840 		return -ENXIO;
841 
842 	srcu_key = srcu_read_lock(&dev->disassociate_srcu);
843 	mutex_lock(&dev->lists_mutex);
844 	ib_dev = srcu_dereference(dev->ib_dev,
845 				  &dev->disassociate_srcu);
846 	if (!ib_dev) {
847 		ret = -EIO;
848 		goto err;
849 	}
850 
851 	/* In case IB device supports disassociate ucontext, there is no hard
852 	 * dependency between uverbs device and its low level device.
853 	 */
854 	module_dependent = !(ib_dev->disassociate_ucontext);
855 
856 	if (module_dependent) {
857 		if (!try_module_get(ib_dev->owner)) {
858 			ret = -ENODEV;
859 			goto err;
860 		}
861 	}
862 
863 	file = kzalloc(sizeof(*file), GFP_KERNEL);
864 	if (!file) {
865 		ret = -ENOMEM;
866 		if (module_dependent)
867 			goto err_module;
868 
869 		goto err;
870 	}
871 
872 	file->device	 = dev;
873 	kref_init(&file->ref);
874 	mutex_init(&file->ucontext_lock);
875 
876 	spin_lock_init(&file->uobjects_lock);
877 	INIT_LIST_HEAD(&file->uobjects);
878 	init_rwsem(&file->hw_destroy_rwsem);
879 
880 	filp->private_data = file;
881 	kobject_get(&dev->kobj);
882 	list_add_tail(&file->list, &dev->uverbs_file_list);
883 	mutex_unlock(&dev->lists_mutex);
884 	srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
885 
886 	file->uverbs_cmd_mask = ib_dev->uverbs_cmd_mask;
887 	file->uverbs_ex_cmd_mask = ib_dev->uverbs_ex_cmd_mask;
888 
889 	setup_ufile_idr_uobject(file);
890 
891 	return nonseekable_open(inode, filp);
892 
893 err_module:
894 	module_put(ib_dev->owner);
895 
896 err:
897 	mutex_unlock(&dev->lists_mutex);
898 	srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
899 	if (atomic_dec_and_test(&dev->refcount))
900 		ib_uverbs_comp_dev(dev);
901 
902 	return ret;
903 }
904 
905 static int ib_uverbs_close(struct inode *inode, struct file *filp)
906 {
907 	struct ib_uverbs_file *file = filp->private_data;
908 
909 	uverbs_destroy_ufile_hw(file, RDMA_REMOVE_CLOSE);
910 
911 	mutex_lock(&file->device->lists_mutex);
912 	if (!file->is_closed) {
913 		list_del(&file->list);
914 		file->is_closed = 1;
915 	}
916 	mutex_unlock(&file->device->lists_mutex);
917 
918 	if (file->async_file)
919 		kref_put(&file->async_file->ref,
920 			 ib_uverbs_release_async_event_file);
921 
922 	kref_put(&file->ref, ib_uverbs_release_file);
923 
924 	return 0;
925 }
926 
927 static const struct file_operations uverbs_fops = {
928 	.owner	 = THIS_MODULE,
929 	.write	 = ib_uverbs_write,
930 	.open	 = ib_uverbs_open,
931 	.release = ib_uverbs_close,
932 	.llseek	 = no_llseek,
933 	.unlocked_ioctl = ib_uverbs_ioctl,
934 	.compat_ioctl = ib_uverbs_ioctl,
935 };
936 
937 static const struct file_operations uverbs_mmap_fops = {
938 	.owner	 = THIS_MODULE,
939 	.write	 = ib_uverbs_write,
940 	.mmap    = ib_uverbs_mmap,
941 	.open	 = ib_uverbs_open,
942 	.release = ib_uverbs_close,
943 	.llseek	 = no_llseek,
944 	.unlocked_ioctl = ib_uverbs_ioctl,
945 	.compat_ioctl = ib_uverbs_ioctl,
946 };
947 
948 static struct ib_client uverbs_client = {
949 	.name   = "uverbs",
950 	.add    = ib_uverbs_add_one,
951 	.remove = ib_uverbs_remove_one
952 };
953 
954 static ssize_t show_ibdev(struct device *device, struct device_attribute *attr,
955 			  char *buf)
956 {
957 	int ret = -ENODEV;
958 	int srcu_key;
959 	struct ib_uverbs_device *dev = dev_get_drvdata(device);
960 	struct ib_device *ib_dev;
961 
962 	if (!dev)
963 		return -ENODEV;
964 
965 	srcu_key = srcu_read_lock(&dev->disassociate_srcu);
966 	ib_dev = srcu_dereference(dev->ib_dev, &dev->disassociate_srcu);
967 	if (ib_dev)
968 		ret = sprintf(buf, "%s\n", ib_dev->name);
969 	srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
970 
971 	return ret;
972 }
973 static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
974 
975 static ssize_t show_dev_abi_version(struct device *device,
976 				    struct device_attribute *attr, char *buf)
977 {
978 	struct ib_uverbs_device *dev = dev_get_drvdata(device);
979 	int ret = -ENODEV;
980 	int srcu_key;
981 	struct ib_device *ib_dev;
982 
983 	if (!dev)
984 		return -ENODEV;
985 	srcu_key = srcu_read_lock(&dev->disassociate_srcu);
986 	ib_dev = srcu_dereference(dev->ib_dev, &dev->disassociate_srcu);
987 	if (ib_dev)
988 		ret = sprintf(buf, "%d\n", ib_dev->uverbs_abi_ver);
989 	srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
990 
991 	return ret;
992 }
993 static DEVICE_ATTR(abi_version, S_IRUGO, show_dev_abi_version, NULL);
994 
995 static CLASS_ATTR_STRING(abi_version, S_IRUGO,
996 			 __stringify(IB_USER_VERBS_ABI_VERSION));
997 
998 static int ib_uverbs_create_uapi(struct ib_device *device,
999 				 struct ib_uverbs_device *uverbs_dev)
1000 {
1001 	struct uverbs_api *uapi;
1002 
1003 	uapi = uverbs_alloc_api(device->driver_specs, device->driver_id);
1004 	if (IS_ERR(uapi))
1005 		return PTR_ERR(uapi);
1006 
1007 	uverbs_dev->uapi = uapi;
1008 	return 0;
1009 }
1010 
1011 static void ib_uverbs_add_one(struct ib_device *device)
1012 {
1013 	int devnum;
1014 	dev_t base;
1015 	struct ib_uverbs_device *uverbs_dev;
1016 	int ret;
1017 
1018 	if (!device->alloc_ucontext)
1019 		return;
1020 
1021 	uverbs_dev = kzalloc(sizeof(*uverbs_dev), GFP_KERNEL);
1022 	if (!uverbs_dev)
1023 		return;
1024 
1025 	ret = init_srcu_struct(&uverbs_dev->disassociate_srcu);
1026 	if (ret) {
1027 		kfree(uverbs_dev);
1028 		return;
1029 	}
1030 
1031 	atomic_set(&uverbs_dev->refcount, 1);
1032 	init_completion(&uverbs_dev->comp);
1033 	uverbs_dev->xrcd_tree = RB_ROOT;
1034 	mutex_init(&uverbs_dev->xrcd_tree_mutex);
1035 	kobject_init(&uverbs_dev->kobj, &ib_uverbs_dev_ktype);
1036 	mutex_init(&uverbs_dev->lists_mutex);
1037 	INIT_LIST_HEAD(&uverbs_dev->uverbs_file_list);
1038 	INIT_LIST_HEAD(&uverbs_dev->uverbs_events_file_list);
1039 
1040 	devnum = find_first_zero_bit(dev_map, IB_UVERBS_MAX_DEVICES);
1041 	if (devnum >= IB_UVERBS_MAX_DEVICES)
1042 		goto err;
1043 	uverbs_dev->devnum = devnum;
1044 	set_bit(devnum, dev_map);
1045 	if (devnum >= IB_UVERBS_NUM_FIXED_MINOR)
1046 		base = dynamic_uverbs_dev + devnum - IB_UVERBS_NUM_FIXED_MINOR;
1047 	else
1048 		base = IB_UVERBS_BASE_DEV + devnum;
1049 
1050 	rcu_assign_pointer(uverbs_dev->ib_dev, device);
1051 	uverbs_dev->num_comp_vectors = device->num_comp_vectors;
1052 
1053 	if (ib_uverbs_create_uapi(device, uverbs_dev))
1054 		goto err_uapi;
1055 
1056 	cdev_init(&uverbs_dev->cdev, NULL);
1057 	uverbs_dev->cdev.owner = THIS_MODULE;
1058 	uverbs_dev->cdev.ops = device->mmap ? &uverbs_mmap_fops : &uverbs_fops;
1059 	cdev_set_parent(&uverbs_dev->cdev, &uverbs_dev->kobj);
1060 	kobject_set_name(&uverbs_dev->cdev.kobj, "uverbs%d", uverbs_dev->devnum);
1061 	if (cdev_add(&uverbs_dev->cdev, base, 1))
1062 		goto err_cdev;
1063 
1064 	uverbs_dev->dev = device_create(uverbs_class, device->dev.parent,
1065 					uverbs_dev->cdev.dev, uverbs_dev,
1066 					"uverbs%d", uverbs_dev->devnum);
1067 	if (IS_ERR(uverbs_dev->dev))
1068 		goto err_cdev;
1069 
1070 	if (device_create_file(uverbs_dev->dev, &dev_attr_ibdev))
1071 		goto err_class;
1072 	if (device_create_file(uverbs_dev->dev, &dev_attr_abi_version))
1073 		goto err_class;
1074 
1075 	ib_set_client_data(device, &uverbs_client, uverbs_dev);
1076 
1077 	return;
1078 
1079 err_class:
1080 	device_destroy(uverbs_class, uverbs_dev->cdev.dev);
1081 err_cdev:
1082 	cdev_del(&uverbs_dev->cdev);
1083 err_uapi:
1084 	clear_bit(devnum, dev_map);
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