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