]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/infiniband/core/uverbs_main.c
IB/uverbs: Fix reference counting usage of event files
[mirror_ubuntu-bionic-kernel.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
RD
48
49#include <asm/uaccess.h>
50
51#include "uverbs.h"
52
53MODULE_AUTHOR("Roland Dreier");
54MODULE_DESCRIPTION("InfiniBand userspace verbs access");
55MODULE_LICENSE("Dual BSD/GPL");
56
bc38a6ab
RD
57enum {
58 IB_UVERBS_MAJOR = 231,
59 IB_UVERBS_BASE_MINOR = 192,
60 IB_UVERBS_MAX_DEVICES = 32
61};
62
63#define IB_UVERBS_BASE_DEV MKDEV(IB_UVERBS_MAJOR, IB_UVERBS_BASE_MINOR)
64
70a30e16
RD
65static struct class *uverbs_class;
66
9ead190b 67DEFINE_SPINLOCK(ib_uverbs_idr_lock);
bc38a6ab
RD
68DEFINE_IDR(ib_uverbs_pd_idr);
69DEFINE_IDR(ib_uverbs_mr_idr);
70DEFINE_IDR(ib_uverbs_mw_idr);
71DEFINE_IDR(ib_uverbs_ah_idr);
72DEFINE_IDR(ib_uverbs_cq_idr);
73DEFINE_IDR(ib_uverbs_qp_idr);
f520ba5a 74DEFINE_IDR(ib_uverbs_srq_idr);
53d0bd1e 75DEFINE_IDR(ib_uverbs_xrcd_idr);
436f2ad0 76DEFINE_IDR(ib_uverbs_rule_idr);
bc38a6ab 77
6276e08a 78static DEFINE_SPINLOCK(map_lock);
bc38a6ab
RD
79static DECLARE_BITMAP(dev_map, IB_UVERBS_MAX_DEVICES);
80
81static ssize_t (*uverbs_cmd_table[])(struct ib_uverbs_file *file,
82 const char __user *buf, int in_len,
83 int out_len) = {
9afed76d
AC
84 [IB_USER_VERBS_CMD_GET_CONTEXT] = ib_uverbs_get_context,
85 [IB_USER_VERBS_CMD_QUERY_DEVICE] = ib_uverbs_query_device,
86 [IB_USER_VERBS_CMD_QUERY_PORT] = ib_uverbs_query_port,
87 [IB_USER_VERBS_CMD_ALLOC_PD] = ib_uverbs_alloc_pd,
88 [IB_USER_VERBS_CMD_DEALLOC_PD] = ib_uverbs_dealloc_pd,
89 [IB_USER_VERBS_CMD_REG_MR] = ib_uverbs_reg_mr,
7e6edb9b 90 [IB_USER_VERBS_CMD_REREG_MR] = ib_uverbs_rereg_mr,
9afed76d 91 [IB_USER_VERBS_CMD_DEREG_MR] = ib_uverbs_dereg_mr,
6b52a12b
SM
92 [IB_USER_VERBS_CMD_ALLOC_MW] = ib_uverbs_alloc_mw,
93 [IB_USER_VERBS_CMD_DEALLOC_MW] = ib_uverbs_dealloc_mw,
6b73597e 94 [IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL] = ib_uverbs_create_comp_channel,
9afed76d
AC
95 [IB_USER_VERBS_CMD_CREATE_CQ] = ib_uverbs_create_cq,
96 [IB_USER_VERBS_CMD_RESIZE_CQ] = ib_uverbs_resize_cq,
97 [IB_USER_VERBS_CMD_POLL_CQ] = ib_uverbs_poll_cq,
98 [IB_USER_VERBS_CMD_REQ_NOTIFY_CQ] = ib_uverbs_req_notify_cq,
99 [IB_USER_VERBS_CMD_DESTROY_CQ] = ib_uverbs_destroy_cq,
100 [IB_USER_VERBS_CMD_CREATE_QP] = ib_uverbs_create_qp,
101 [IB_USER_VERBS_CMD_QUERY_QP] = ib_uverbs_query_qp,
102 [IB_USER_VERBS_CMD_MODIFY_QP] = ib_uverbs_modify_qp,
103 [IB_USER_VERBS_CMD_DESTROY_QP] = ib_uverbs_destroy_qp,
104 [IB_USER_VERBS_CMD_POST_SEND] = ib_uverbs_post_send,
105 [IB_USER_VERBS_CMD_POST_RECV] = ib_uverbs_post_recv,
106 [IB_USER_VERBS_CMD_POST_SRQ_RECV] = ib_uverbs_post_srq_recv,
107 [IB_USER_VERBS_CMD_CREATE_AH] = ib_uverbs_create_ah,
108 [IB_USER_VERBS_CMD_DESTROY_AH] = ib_uverbs_destroy_ah,
109 [IB_USER_VERBS_CMD_ATTACH_MCAST] = ib_uverbs_attach_mcast,
110 [IB_USER_VERBS_CMD_DETACH_MCAST] = ib_uverbs_detach_mcast,
111 [IB_USER_VERBS_CMD_CREATE_SRQ] = ib_uverbs_create_srq,
112 [IB_USER_VERBS_CMD_MODIFY_SRQ] = ib_uverbs_modify_srq,
113 [IB_USER_VERBS_CMD_QUERY_SRQ] = ib_uverbs_query_srq,
114 [IB_USER_VERBS_CMD_DESTROY_SRQ] = ib_uverbs_destroy_srq,
53d0bd1e
SH
115 [IB_USER_VERBS_CMD_OPEN_XRCD] = ib_uverbs_open_xrcd,
116 [IB_USER_VERBS_CMD_CLOSE_XRCD] = ib_uverbs_close_xrcd,
42849b26 117 [IB_USER_VERBS_CMD_CREATE_XSRQ] = ib_uverbs_create_xsrq,
436f2ad0 118 [IB_USER_VERBS_CMD_OPEN_QP] = ib_uverbs_open_qp,
f21519b2
YD
119};
120
f21519b2
YD
121static int (*uverbs_ex_cmd_table[])(struct ib_uverbs_file *file,
122 struct ib_udata *ucore,
123 struct ib_udata *uhw) = {
124 [IB_USER_VERBS_EX_CMD_CREATE_FLOW] = ib_uverbs_ex_create_flow,
5a77abf9 125 [IB_USER_VERBS_EX_CMD_DESTROY_FLOW] = ib_uverbs_ex_destroy_flow,
02d1aa7a 126 [IB_USER_VERBS_EX_CMD_QUERY_DEVICE] = ib_uverbs_ex_query_device,
565197dd 127 [IB_USER_VERBS_EX_CMD_CREATE_CQ] = ib_uverbs_ex_create_cq,
bc38a6ab
RD
128};
129
bc38a6ab 130static void ib_uverbs_add_one(struct ib_device *device);
7c1eb45a 131static void ib_uverbs_remove_one(struct ib_device *device, void *client_data);
bc38a6ab 132
70a30e16
RD
133static void ib_uverbs_release_dev(struct kref *ref)
134{
135 struct ib_uverbs_device *dev =
136 container_of(ref, struct ib_uverbs_device, ref);
137
fd60ae40 138 complete(&dev->comp);
70a30e16
RD
139}
140
04d29b0e
RD
141static void ib_uverbs_release_event_file(struct kref *ref)
142{
143 struct ib_uverbs_event_file *file =
144 container_of(ref, struct ib_uverbs_event_file, ref);
145
146 kfree(file);
147}
148
70a30e16
RD
149void ib_uverbs_release_ucq(struct ib_uverbs_file *file,
150 struct ib_uverbs_event_file *ev_file,
151 struct ib_ucq_object *uobj)
152{
153 struct ib_uverbs_event *evt, *tmp;
154
155 if (ev_file) {
156 spin_lock_irq(&ev_file->lock);
157 list_for_each_entry_safe(evt, tmp, &uobj->comp_list, obj_list) {
158 list_del(&evt->list);
159 kfree(evt);
160 }
161 spin_unlock_irq(&ev_file->lock);
162
163 kref_put(&ev_file->ref, ib_uverbs_release_event_file);
164 }
165
166 spin_lock_irq(&file->async_file->lock);
167 list_for_each_entry_safe(evt, tmp, &uobj->async_list, obj_list) {
168 list_del(&evt->list);
169 kfree(evt);
170 }
171 spin_unlock_irq(&file->async_file->lock);
172}
173
174void ib_uverbs_release_uevent(struct ib_uverbs_file *file,
175 struct ib_uevent_object *uobj)
176{
177 struct ib_uverbs_event *evt, *tmp;
178
179 spin_lock_irq(&file->async_file->lock);
180 list_for_each_entry_safe(evt, tmp, &uobj->event_list, obj_list) {
181 list_del(&evt->list);
182 kfree(evt);
183 }
184 spin_unlock_irq(&file->async_file->lock);
185}
186
f4e40156
JM
187static void ib_uverbs_detach_umcast(struct ib_qp *qp,
188 struct ib_uqp_object *uobj)
189{
190 struct ib_uverbs_mcast_entry *mcast, *tmp;
191
192 list_for_each_entry_safe(mcast, tmp, &uobj->mcast_list, list) {
193 ib_detach_mcast(qp, &mcast->gid, mcast->lid);
194 list_del(&mcast->list);
195 kfree(mcast);
196 }
197}
198
70a30e16
RD
199static int ib_uverbs_cleanup_ucontext(struct ib_uverbs_file *file,
200 struct ib_ucontext *context)
bc38a6ab
RD
201{
202 struct ib_uobject *uobj, *tmp;
203
204 if (!context)
205 return 0;
206
f7c6a7b5
RD
207 context->closing = 1;
208
67cdb40c 209 list_for_each_entry_safe(uobj, tmp, &context->ah_list, list) {
9ead190b
RD
210 struct ib_ah *ah = uobj->object;
211
212 idr_remove_uobj(&ib_uverbs_ah_idr, uobj);
67cdb40c 213 ib_destroy_ah(ah);
67cdb40c
RD
214 kfree(uobj);
215 }
bc38a6ab 216
6b52a12b
SM
217 /* Remove MWs before QPs, in order to support type 2A MWs. */
218 list_for_each_entry_safe(uobj, tmp, &context->mw_list, list) {
219 struct ib_mw *mw = uobj->object;
220
221 idr_remove_uobj(&ib_uverbs_mw_idr, uobj);
222 ib_dealloc_mw(mw);
223 kfree(uobj);
224 }
225
436f2ad0
HHZ
226 list_for_each_entry_safe(uobj, tmp, &context->rule_list, list) {
227 struct ib_flow *flow_id = uobj->object;
228
229 idr_remove_uobj(&ib_uverbs_rule_idr, uobj);
230 ib_destroy_flow(flow_id);
231 kfree(uobj);
232 }
233
bc38a6ab 234 list_for_each_entry_safe(uobj, tmp, &context->qp_list, list) {
9ead190b 235 struct ib_qp *qp = uobj->object;
f4e40156
JM
236 struct ib_uqp_object *uqp =
237 container_of(uobj, struct ib_uqp_object, uevent.uobject);
9ead190b
RD
238
239 idr_remove_uobj(&ib_uverbs_qp_idr, uobj);
0e0ec7e0
SH
240 if (qp != qp->real_qp) {
241 ib_close_qp(qp);
b93f3c18
SH
242 } else {
243 ib_uverbs_detach_umcast(qp, uqp);
244 ib_destroy_qp(qp);
245 }
f4e40156
JM
246 ib_uverbs_release_uevent(file, &uqp->uevent);
247 kfree(uqp);
bc38a6ab
RD
248 }
249
a233c4b5
SD
250 list_for_each_entry_safe(uobj, tmp, &context->srq_list, list) {
251 struct ib_srq *srq = uobj->object;
252 struct ib_uevent_object *uevent =
253 container_of(uobj, struct ib_uevent_object, uobject);
254
255 idr_remove_uobj(&ib_uverbs_srq_idr, uobj);
256 ib_destroy_srq(srq);
257 ib_uverbs_release_uevent(file, uevent);
258 kfree(uevent);
259 }
260
bc38a6ab 261 list_for_each_entry_safe(uobj, tmp, &context->cq_list, list) {
9ead190b 262 struct ib_cq *cq = uobj->object;
70a30e16
RD
263 struct ib_uverbs_event_file *ev_file = cq->cq_context;
264 struct ib_ucq_object *ucq =
265 container_of(uobj, struct ib_ucq_object, uobject);
9ead190b
RD
266
267 idr_remove_uobj(&ib_uverbs_cq_idr, uobj);
bc38a6ab 268 ib_destroy_cq(cq);
70a30e16
RD
269 ib_uverbs_release_ucq(file, ev_file, ucq);
270 kfree(ucq);
bc38a6ab
RD
271 }
272
bc38a6ab 273 list_for_each_entry_safe(uobj, tmp, &context->mr_list, list) {
9ead190b 274 struct ib_mr *mr = uobj->object;
bc38a6ab 275
9ead190b 276 idr_remove_uobj(&ib_uverbs_mr_idr, uobj);
bc38a6ab 277 ib_dereg_mr(mr);
f7c6a7b5 278 kfree(uobj);
bc38a6ab
RD
279 }
280
53d0bd1e
SH
281 mutex_lock(&file->device->xrcd_tree_mutex);
282 list_for_each_entry_safe(uobj, tmp, &context->xrcd_list, list) {
283 struct ib_xrcd *xrcd = uobj->object;
284 struct ib_uxrcd_object *uxrcd =
285 container_of(uobj, struct ib_uxrcd_object, uobject);
286
287 idr_remove_uobj(&ib_uverbs_xrcd_idr, uobj);
288 ib_uverbs_dealloc_xrcd(file->device, xrcd);
289 kfree(uxrcd);
290 }
291 mutex_unlock(&file->device->xrcd_tree_mutex);
292
bc38a6ab 293 list_for_each_entry_safe(uobj, tmp, &context->pd_list, list) {
9ead190b
RD
294 struct ib_pd *pd = uobj->object;
295
296 idr_remove_uobj(&ib_uverbs_pd_idr, uobj);
bc38a6ab 297 ib_dealloc_pd(pd);
bc38a6ab
RD
298 kfree(uobj);
299 }
300
8ada2c1c
SR
301 put_pid(context->tgid);
302
bc38a6ab
RD
303 return context->device->dealloc_ucontext(context);
304}
305
306static void ib_uverbs_release_file(struct kref *ref)
307{
308 struct ib_uverbs_file *file =
309 container_of(ref, struct ib_uverbs_file, ref);
310
311 module_put(file->device->ib_dev->owner);
70a30e16
RD
312 kref_put(&file->device->ref, ib_uverbs_release_dev);
313
bc38a6ab
RD
314 kfree(file);
315}
316
317static ssize_t ib_uverbs_event_read(struct file *filp, char __user *buf,
318 size_t count, loff_t *pos)
319{
320 struct ib_uverbs_event_file *file = filp->private_data;
63aaf647 321 struct ib_uverbs_event *event;
bc38a6ab
RD
322 int eventsz;
323 int ret = 0;
324
325 spin_lock_irq(&file->lock);
326
6b73597e 327 while (list_empty(&file->event_list)) {
bc38a6ab
RD
328 spin_unlock_irq(&file->lock);
329
330 if (filp->f_flags & O_NONBLOCK)
331 return -EAGAIN;
332
333 if (wait_event_interruptible(file->poll_wait,
6b73597e 334 !list_empty(&file->event_list)))
bc38a6ab
RD
335 return -ERESTARTSYS;
336
337 spin_lock_irq(&file->lock);
338 }
339
63aaf647
RD
340 event = list_entry(file->event_list.next, struct ib_uverbs_event, list);
341
342 if (file->is_async)
bc38a6ab 343 eventsz = sizeof (struct ib_uverbs_async_event_desc);
63aaf647 344 else
bc38a6ab 345 eventsz = sizeof (struct ib_uverbs_comp_event_desc);
bc38a6ab
RD
346
347 if (eventsz > count) {
348 ret = -EINVAL;
349 event = NULL;
63aaf647 350 } else {
bc38a6ab 351 list_del(file->event_list.next);
63aaf647
RD
352 if (event->counter) {
353 ++(*event->counter);
354 list_del(&event->obj_list);
355 }
356 }
bc38a6ab
RD
357
358 spin_unlock_irq(&file->lock);
359
360 if (event) {
361 if (copy_to_user(buf, event, eventsz))
362 ret = -EFAULT;
363 else
364 ret = eventsz;
365 }
366
367 kfree(event);
368
369 return ret;
370}
371
372static unsigned int ib_uverbs_event_poll(struct file *filp,
373 struct poll_table_struct *wait)
374{
375 unsigned int pollflags = 0;
376 struct ib_uverbs_event_file *file = filp->private_data;
377
378 poll_wait(filp, &file->poll_wait, wait);
379
380 spin_lock_irq(&file->lock);
6b73597e 381 if (!list_empty(&file->event_list))
bc38a6ab
RD
382 pollflags = POLLIN | POLLRDNORM;
383 spin_unlock_irq(&file->lock);
384
385 return pollflags;
386}
387
abdf119b
GN
388static int ib_uverbs_event_fasync(int fd, struct file *filp, int on)
389{
390 struct ib_uverbs_event_file *file = filp->private_data;
391
392 return fasync_helper(fd, filp, on, &file->async_queue);
393}
394
bc38a6ab
RD
395static int ib_uverbs_event_close(struct inode *inode, struct file *filp)
396{
397 struct ib_uverbs_event_file *file = filp->private_data;
6b73597e
RD
398 struct ib_uverbs_event *entry, *tmp;
399
400 spin_lock_irq(&file->lock);
1ae5c187 401 file->is_closed = 1;
6b73597e
RD
402 list_for_each_entry_safe(entry, tmp, &file->event_list, list) {
403 if (entry->counter)
404 list_del(&entry->obj_list);
405 kfree(entry);
406 }
407 spin_unlock_irq(&file->lock);
bc38a6ab 408
03c40442 409 if (file->is_async)
6b73597e 410 ib_unregister_event_handler(&file->uverbs_file->event_handler);
03c40442 411 kref_put(&file->uverbs_file->ref, ib_uverbs_release_file);
6b73597e 412 kref_put(&file->ref, ib_uverbs_release_event_file);
bc38a6ab
RD
413
414 return 0;
415}
416
2b8693c0 417static const struct file_operations uverbs_event_fops = {
6b73597e 418 .owner = THIS_MODULE,
9afed76d 419 .read = ib_uverbs_event_read,
bc38a6ab 420 .poll = ib_uverbs_event_poll,
abdf119b 421 .release = ib_uverbs_event_close,
bc1db9af
RD
422 .fasync = ib_uverbs_event_fasync,
423 .llseek = no_llseek,
bc38a6ab
RD
424};
425
426void ib_uverbs_comp_handler(struct ib_cq *cq, void *cq_context)
427{
6b73597e
RD
428 struct ib_uverbs_event_file *file = cq_context;
429 struct ib_ucq_object *uobj;
430 struct ib_uverbs_event *entry;
431 unsigned long flags;
432
433 if (!file)
434 return;
435
436 spin_lock_irqsave(&file->lock, flags);
1ae5c187 437 if (file->is_closed) {
6b73597e
RD
438 spin_unlock_irqrestore(&file->lock, flags);
439 return;
440 }
bc38a6ab
RD
441
442 entry = kmalloc(sizeof *entry, GFP_ATOMIC);
305a7e87
RD
443 if (!entry) {
444 spin_unlock_irqrestore(&file->lock, flags);
bc38a6ab 445 return;
305a7e87 446 }
bc38a6ab 447
63aaf647
RD
448 uobj = container_of(cq->uobject, struct ib_ucq_object, uobject);
449
450 entry->desc.comp.cq_handle = cq->uobject->user_handle;
451 entry->counter = &uobj->comp_events_reported;
bc38a6ab 452
6b73597e 453 list_add_tail(&entry->list, &file->event_list);
63aaf647 454 list_add_tail(&entry->obj_list, &uobj->comp_list);
6b73597e 455 spin_unlock_irqrestore(&file->lock, flags);
bc38a6ab 456
6b73597e
RD
457 wake_up_interruptible(&file->poll_wait);
458 kill_fasync(&file->async_queue, SIGIO, POLL_IN);
bc38a6ab
RD
459}
460
461static void ib_uverbs_async_handler(struct ib_uverbs_file *file,
63aaf647
RD
462 __u64 element, __u64 event,
463 struct list_head *obj_list,
464 u32 *counter)
bc38a6ab 465{
63aaf647 466 struct ib_uverbs_event *entry;
bc38a6ab
RD
467 unsigned long flags;
468
6b73597e 469 spin_lock_irqsave(&file->async_file->lock, flags);
fb77bcef 470 if (file->async_file->is_closed) {
6b73597e
RD
471 spin_unlock_irqrestore(&file->async_file->lock, flags);
472 return;
473 }
474
bc38a6ab 475 entry = kmalloc(sizeof *entry, GFP_ATOMIC);
305a7e87
RD
476 if (!entry) {
477 spin_unlock_irqrestore(&file->async_file->lock, flags);
bc38a6ab 478 return;
305a7e87 479 }
bc38a6ab 480
63aaf647
RD
481 entry->desc.async.element = element;
482 entry->desc.async.event_type = event;
377b5134 483 entry->desc.async.reserved = 0;
63aaf647 484 entry->counter = counter;
bc38a6ab 485
6b73597e 486 list_add_tail(&entry->list, &file->async_file->event_list);
63aaf647
RD
487 if (obj_list)
488 list_add_tail(&entry->obj_list, obj_list);
6b73597e 489 spin_unlock_irqrestore(&file->async_file->lock, flags);
bc38a6ab 490
6b73597e
RD
491 wake_up_interruptible(&file->async_file->poll_wait);
492 kill_fasync(&file->async_file->async_queue, SIGIO, POLL_IN);
bc38a6ab
RD
493}
494
495void ib_uverbs_cq_event_handler(struct ib_event *event, void *context_ptr)
496{
7162a3e0
RD
497 struct ib_ucq_object *uobj = container_of(event->element.cq->uobject,
498 struct ib_ucq_object, uobject);
63aaf647 499
7162a3e0 500 ib_uverbs_async_handler(uobj->uverbs_file, uobj->uobject.user_handle,
63aaf647
RD
501 event->event, &uobj->async_list,
502 &uobj->async_events_reported);
bc38a6ab
RD
503}
504
505void ib_uverbs_qp_event_handler(struct ib_event *event, void *context_ptr)
506{
63aaf647
RD
507 struct ib_uevent_object *uobj;
508
a040f95d
JM
509 /* for XRC target qp's, check that qp is live */
510 if (!event->element.qp->uobject || !event->element.qp->uobject->live)
511 return;
512
63aaf647
RD
513 uobj = container_of(event->element.qp->uobject,
514 struct ib_uevent_object, uobject);
515
516 ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle,
517 event->event, &uobj->event_list,
518 &uobj->events_reported);
bc38a6ab
RD
519}
520
f520ba5a
RD
521void ib_uverbs_srq_event_handler(struct ib_event *event, void *context_ptr)
522{
63aaf647
RD
523 struct ib_uevent_object *uobj;
524
525 uobj = container_of(event->element.srq->uobject,
526 struct ib_uevent_object, uobject);
527
528 ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle,
529 event->event, &uobj->event_list,
530 &uobj->events_reported);
f520ba5a
RD
531}
532
6b73597e
RD
533void ib_uverbs_event_handler(struct ib_event_handler *handler,
534 struct ib_event *event)
bc38a6ab
RD
535{
536 struct ib_uverbs_file *file =
537 container_of(handler, struct ib_uverbs_file, event_handler);
538
63aaf647
RD
539 ib_uverbs_async_handler(file, event->element.port_num, event->event,
540 NULL, NULL);
bc38a6ab
RD
541}
542
03c40442
YH
543void ib_uverbs_free_async_event_file(struct ib_uverbs_file *file)
544{
545 kref_put(&file->async_file->ref, ib_uverbs_release_event_file);
546 file->async_file = NULL;
547}
548
6b73597e 549struct file *ib_uverbs_alloc_event_file(struct ib_uverbs_file *uverbs_file,
b1e4594b 550 int is_async)
bc38a6ab 551{
6b73597e 552 struct ib_uverbs_event_file *ev_file;
bc38a6ab 553 struct file *filp;
03c40442 554 int ret;
bc38a6ab 555
03c40442 556 ev_file = kzalloc(sizeof(*ev_file), GFP_KERNEL);
6b73597e
RD
557 if (!ev_file)
558 return ERR_PTR(-ENOMEM);
559
560 kref_init(&ev_file->ref);
561 spin_lock_init(&ev_file->lock);
562 INIT_LIST_HEAD(&ev_file->event_list);
563 init_waitqueue_head(&ev_file->poll_wait);
564 ev_file->uverbs_file = uverbs_file;
03c40442 565 kref_get(&ev_file->uverbs_file->ref);
6b73597e 566 ev_file->async_queue = NULL;
1ae5c187 567 ev_file->is_closed = 0;
6b73597e 568
b1e4594b 569 filp = anon_inode_getfile("[infinibandevent]", &uverbs_event_fops,
a265e558 570 ev_file, O_RDONLY);
b1e4594b 571 if (IS_ERR(filp))
03c40442
YH
572 goto err_put_refs;
573
574 if (is_async) {
575 WARN_ON(uverbs_file->async_file);
576 uverbs_file->async_file = ev_file;
577 kref_get(&uverbs_file->async_file->ref);
578 INIT_IB_EVENT_HANDLER(&uverbs_file->event_handler,
579 uverbs_file->device->ib_dev,
580 ib_uverbs_event_handler);
581 ret = ib_register_event_handler(&uverbs_file->event_handler);
582 if (ret)
583 goto err_put_file;
584
585 /* At that point async file stuff was fully set */
586 ev_file->is_async = 1;
587 }
588
589 return filp;
590
591err_put_file:
592 fput(filp);
593 kref_put(&uverbs_file->async_file->ref, ib_uverbs_release_event_file);
594 uverbs_file->async_file = NULL;
595 return ERR_PTR(ret);
a7dab9e8 596
03c40442
YH
597err_put_refs:
598 kref_put(&ev_file->uverbs_file->ref, ib_uverbs_release_file);
599 kref_put(&ev_file->ref, ib_uverbs_release_event_file);
6b73597e 600 return filp;
6b73597e
RD
601}
602
603/*
604 * Look up a completion event file by FD. If lookup is successful,
605 * takes a ref to the event file struct that it returns; if
606 * unsuccessful, returns NULL.
607 */
608struct ib_uverbs_event_file *ib_uverbs_lookup_comp_file(int fd)
609{
610 struct ib_uverbs_event_file *ev_file = NULL;
2903ff01 611 struct fd f = fdget(fd);
6b73597e 612
2903ff01 613 if (!f.file)
6b73597e
RD
614 return NULL;
615
2903ff01 616 if (f.file->f_op != &uverbs_event_fops)
6b73597e
RD
617 goto out;
618
2903ff01 619 ev_file = f.file->private_data;
6b73597e
RD
620 if (ev_file->is_async) {
621 ev_file = NULL;
622 goto out;
623 }
624
625 kref_get(&ev_file->ref);
626
627out:
2903ff01 628 fdput(f);
6b73597e 629 return ev_file;
bc38a6ab
RD
630}
631
632static ssize_t ib_uverbs_write(struct file *filp, const char __user *buf,
633 size_t count, loff_t *pos)
634{
635 struct ib_uverbs_file *file = filp->private_data;
636 struct ib_uverbs_cmd_hdr hdr;
f21519b2 637 __u32 flags;
bc38a6ab
RD
638
639 if (count < sizeof hdr)
640 return -EINVAL;
641
642 if (copy_from_user(&hdr, buf, sizeof hdr))
643 return -EFAULT;
644
f21519b2
YD
645 flags = (hdr.command &
646 IB_USER_VERBS_CMD_FLAGS_MASK) >> IB_USER_VERBS_CMD_FLAGS_SHIFT;
bc38a6ab 647
f21519b2
YD
648 if (!flags) {
649 __u32 command;
bc38a6ab 650
f21519b2
YD
651 if (hdr.command & ~(__u32)(IB_USER_VERBS_CMD_FLAGS_MASK |
652 IB_USER_VERBS_CMD_COMMAND_MASK))
653 return -EINVAL;
b1b8afb8 654
f21519b2 655 command = hdr.command & IB_USER_VERBS_CMD_COMMAND_MASK;
400dbc96 656
f21519b2
YD
657 if (command >= ARRAY_SIZE(uverbs_cmd_table) ||
658 !uverbs_cmd_table[command])
659 return -EINVAL;
400dbc96 660
f21519b2
YD
661 if (!file->ucontext &&
662 command != IB_USER_VERBS_CMD_GET_CONTEXT)
400dbc96
II
663 return -EINVAL;
664
f21519b2
YD
665 if (!(file->device->ib_dev->uverbs_cmd_mask & (1ull << command)))
666 return -ENOSYS;
667
400dbc96
II
668 if (hdr.in_words * 4 != count)
669 return -EINVAL;
670
f21519b2
YD
671 return uverbs_cmd_table[command](file,
672 buf + sizeof(hdr),
673 hdr.in_words * 4,
674 hdr.out_words * 4);
675
f21519b2
YD
676 } else if (flags == IB_USER_VERBS_CMD_FLAG_EXTENDED) {
677 __u32 command;
678
679 struct ib_uverbs_ex_cmd_hdr ex_hdr;
680 struct ib_udata ucore;
681 struct ib_udata uhw;
682 int err;
683 size_t written_count = count;
684
685 if (hdr.command & ~(__u32)(IB_USER_VERBS_CMD_FLAGS_MASK |
686 IB_USER_VERBS_CMD_COMMAND_MASK))
687 return -EINVAL;
688
689 command = hdr.command & IB_USER_VERBS_CMD_COMMAND_MASK;
690
691 if (command >= ARRAY_SIZE(uverbs_ex_cmd_table) ||
692 !uverbs_ex_cmd_table[command])
693 return -ENOSYS;
694
695 if (!file->ucontext)
696 return -EINVAL;
697
698 if (!(file->device->ib_dev->uverbs_ex_cmd_mask & (1ull << command)))
699 return -ENOSYS;
700
701 if (count < (sizeof(hdr) + sizeof(ex_hdr)))
702 return -EINVAL;
703
704 if (copy_from_user(&ex_hdr, buf + sizeof(hdr), sizeof(ex_hdr)))
705 return -EFAULT;
706
707 count -= sizeof(hdr) + sizeof(ex_hdr);
708 buf += sizeof(hdr) + sizeof(ex_hdr);
709
710 if ((hdr.in_words + ex_hdr.provider_in_words) * 8 != count)
711 return -EINVAL;
712
7efb1b19
YD
713 if (ex_hdr.cmd_hdr_reserved)
714 return -EINVAL;
715
f21519b2
YD
716 if (ex_hdr.response) {
717 if (!hdr.out_words && !ex_hdr.provider_out_words)
718 return -EINVAL;
6cc3df84
YD
719
720 if (!access_ok(VERIFY_WRITE,
721 (void __user *) (unsigned long) ex_hdr.response,
722 (hdr.out_words + ex_hdr.provider_out_words) * 8))
723 return -EFAULT;
f21519b2
YD
724 } else {
725 if (hdr.out_words || ex_hdr.provider_out_words)
726 return -EINVAL;
727 }
728
a96e4e2f
RD
729 INIT_UDATA_BUF_OR_NULL(&ucore, buf, (unsigned long) ex_hdr.response,
730 hdr.in_words * 8, hdr.out_words * 8);
731
732 INIT_UDATA_BUF_OR_NULL(&uhw,
733 buf + ucore.inlen,
734 (unsigned long) ex_hdr.response + ucore.outlen,
735 ex_hdr.provider_in_words * 8,
736 ex_hdr.provider_out_words * 8);
f21519b2
YD
737
738 err = uverbs_ex_cmd_table[command](file,
739 &ucore,
740 &uhw);
741
742 if (err)
743 return err;
744
745 return written_count;
400dbc96 746 }
f21519b2
YD
747
748 return -ENOSYS;
bc38a6ab
RD
749}
750
751static int ib_uverbs_mmap(struct file *filp, struct vm_area_struct *vma)
752{
753 struct ib_uverbs_file *file = filp->private_data;
754
755 if (!file->ucontext)
756 return -ENODEV;
757 else
758 return file->device->ib_dev->mmap(file->ucontext, vma);
759}
760
5b2d281a
RD
761/*
762 * ib_uverbs_open() does not need the BKL:
763 *
2a72f212 764 * - the ib_uverbs_device structures are properly reference counted and
5b2d281a
RD
765 * everything else is purely local to the file being created, so
766 * races against other open calls are not a problem;
767 * - there is no ioctl method to race against;
2a72f212
AC
768 * - the open method will either immediately run -ENXIO, or all
769 * required initialization will be done.
5b2d281a 770 */
bc38a6ab
RD
771static int ib_uverbs_open(struct inode *inode, struct file *filp)
772{
70a30e16 773 struct ib_uverbs_device *dev;
bc38a6ab 774 struct ib_uverbs_file *file;
70a30e16 775 int ret;
bc38a6ab 776
2a72f212 777 dev = container_of(inode->i_cdev, struct ib_uverbs_device, cdev);
70a30e16
RD
778 if (dev)
779 kref_get(&dev->ref);
2a72f212 780 else
70a30e16
RD
781 return -ENXIO;
782
783 if (!try_module_get(dev->ib_dev->owner)) {
784 ret = -ENODEV;
785 goto err;
786 }
bc38a6ab 787
6b73597e 788 file = kmalloc(sizeof *file, GFP_KERNEL);
63c47c28 789 if (!file) {
70a30e16
RD
790 ret = -ENOMEM;
791 goto err_module;
63c47c28 792 }
bc38a6ab 793
70a30e16
RD
794 file->device = dev;
795 file->ucontext = NULL;
796 file->async_file = NULL;
bc38a6ab 797 kref_init(&file->ref);
95ed644f 798 mutex_init(&file->mutex);
bc38a6ab 799
bc38a6ab
RD
800 filp->private_data = file;
801
bc1db9af 802 return nonseekable_open(inode, filp);
70a30e16
RD
803
804err_module:
805 module_put(dev->ib_dev->owner);
806
807err:
808 kref_put(&dev->ref, ib_uverbs_release_dev);
70a30e16 809 return ret;
bc38a6ab
RD
810}
811
812static int ib_uverbs_close(struct inode *inode, struct file *filp)
813{
814 struct ib_uverbs_file *file = filp->private_data;
bc38a6ab 815
70a30e16
RD
816 ib_uverbs_cleanup_ucontext(file, file->ucontext);
817
818 if (file->async_file)
819 kref_put(&file->async_file->ref, ib_uverbs_release_event_file);
bc38a6ab 820
bc38a6ab
RD
821 kref_put(&file->ref, ib_uverbs_release_file);
822
823 return 0;
824}
825
2b8693c0 826static const struct file_operations uverbs_fops = {
9afed76d
AC
827 .owner = THIS_MODULE,
828 .write = ib_uverbs_write,
829 .open = ib_uverbs_open,
bc1db9af
RD
830 .release = ib_uverbs_close,
831 .llseek = no_llseek,
bc38a6ab
RD
832};
833
2b8693c0 834static const struct file_operations uverbs_mmap_fops = {
9afed76d
AC
835 .owner = THIS_MODULE,
836 .write = ib_uverbs_write,
bc38a6ab 837 .mmap = ib_uverbs_mmap,
9afed76d 838 .open = ib_uverbs_open,
bc1db9af
RD
839 .release = ib_uverbs_close,
840 .llseek = no_llseek,
bc38a6ab
RD
841};
842
843static struct ib_client uverbs_client = {
844 .name = "uverbs",
845 .add = ib_uverbs_add_one,
846 .remove = ib_uverbs_remove_one
847};
848
f4e91eb4
TJ
849static ssize_t show_ibdev(struct device *device, struct device_attribute *attr,
850 char *buf)
bc38a6ab 851{
f4e91eb4 852 struct ib_uverbs_device *dev = dev_get_drvdata(device);
70a30e16
RD
853
854 if (!dev)
855 return -ENODEV;
bc38a6ab
RD
856
857 return sprintf(buf, "%s\n", dev->ib_dev->name);
858}
f4e91eb4 859static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
bc38a6ab 860
f4e91eb4
TJ
861static ssize_t show_dev_abi_version(struct device *device,
862 struct device_attribute *attr, char *buf)
274c0891 863{
f4e91eb4 864 struct ib_uverbs_device *dev = dev_get_drvdata(device);
70a30e16
RD
865
866 if (!dev)
867 return -ENODEV;
274c0891
RD
868
869 return sprintf(buf, "%d\n", dev->ib_dev->uverbs_abi_ver);
870}
f4e91eb4 871static DEVICE_ATTR(abi_version, S_IRUGO, show_dev_abi_version, NULL);
274c0891 872
0933e2d9
AK
873static CLASS_ATTR_STRING(abi_version, S_IRUGO,
874 __stringify(IB_USER_VERBS_ABI_VERSION));
bc38a6ab 875
6d6a0e71
AC
876static dev_t overflow_maj;
877static DECLARE_BITMAP(overflow_map, IB_UVERBS_MAX_DEVICES);
878
879/*
880 * If we have more than IB_UVERBS_MAX_DEVICES, dynamically overflow by
881 * requesting a new major number and doubling the number of max devices we
882 * support. It's stupid, but simple.
883 */
884static int find_overflow_devnum(void)
885{
886 int ret;
887
888 if (!overflow_maj) {
889 ret = alloc_chrdev_region(&overflow_maj, 0, IB_UVERBS_MAX_DEVICES,
890 "infiniband_verbs");
891 if (ret) {
892 printk(KERN_ERR "user_verbs: couldn't register dynamic device number\n");
893 return ret;
894 }
895 }
896
897 ret = find_first_zero_bit(overflow_map, IB_UVERBS_MAX_DEVICES);
898 if (ret >= IB_UVERBS_MAX_DEVICES)
899 return -1;
900
901 return ret;
902}
903
bc38a6ab
RD
904static void ib_uverbs_add_one(struct ib_device *device)
905{
38707980 906 int devnum;
ddbd6883 907 dev_t base;
bc38a6ab
RD
908 struct ib_uverbs_device *uverbs_dev;
909
910 if (!device->alloc_ucontext)
911 return;
912
de6eb66b 913 uverbs_dev = kzalloc(sizeof *uverbs_dev, GFP_KERNEL);
bc38a6ab
RD
914 if (!uverbs_dev)
915 return;
916
70a30e16 917 kref_init(&uverbs_dev->ref);
fd60ae40 918 init_completion(&uverbs_dev->comp);
53d0bd1e
SH
919 uverbs_dev->xrcd_tree = RB_ROOT;
920 mutex_init(&uverbs_dev->xrcd_tree_mutex);
70a30e16 921
bc38a6ab 922 spin_lock(&map_lock);
38707980
AC
923 devnum = find_first_zero_bit(dev_map, IB_UVERBS_MAX_DEVICES);
924 if (devnum >= IB_UVERBS_MAX_DEVICES) {
bc38a6ab 925 spin_unlock(&map_lock);
6d6a0e71
AC
926 devnum = find_overflow_devnum();
927 if (devnum < 0)
928 goto err;
929
930 spin_lock(&map_lock);
931 uverbs_dev->devnum = devnum + IB_UVERBS_MAX_DEVICES;
932 base = devnum + overflow_maj;
933 set_bit(devnum, overflow_map);
934 } else {
935 uverbs_dev->devnum = devnum;
936 base = devnum + IB_UVERBS_BASE_DEV;
937 set_bit(devnum, dev_map);
bc38a6ab 938 }
bc38a6ab
RD
939 spin_unlock(&map_lock);
940
6b73597e 941 uverbs_dev->ib_dev = device;
f4fd0b22 942 uverbs_dev->num_comp_vectors = device->num_comp_vectors;
bc38a6ab 943
055422dd
AC
944 cdev_init(&uverbs_dev->cdev, NULL);
945 uverbs_dev->cdev.owner = THIS_MODULE;
946 uverbs_dev->cdev.ops = device->mmap ? &uverbs_mmap_fops : &uverbs_fops;
947 kobject_set_name(&uverbs_dev->cdev.kobj, "uverbs%d", uverbs_dev->devnum);
ddbd6883 948 if (cdev_add(&uverbs_dev->cdev, base, 1))
70a30e16 949 goto err_cdev;
bc38a6ab 950
91bd418f 951 uverbs_dev->dev = device_create(uverbs_class, device->dma_device,
055422dd 952 uverbs_dev->cdev.dev, uverbs_dev,
91bd418f 953 "uverbs%d", uverbs_dev->devnum);
f4e91eb4 954 if (IS_ERR(uverbs_dev->dev))
bc38a6ab
RD
955 goto err_cdev;
956
f4e91eb4 957 if (device_create_file(uverbs_dev->dev, &dev_attr_ibdev))
bc38a6ab 958 goto err_class;
f4e91eb4 959 if (device_create_file(uverbs_dev->dev, &dev_attr_abi_version))
274c0891 960 goto err_class;
bc38a6ab
RD
961
962 ib_set_client_data(device, &uverbs_client, uverbs_dev);
963
964 return;
965
966err_class:
055422dd 967 device_destroy(uverbs_class, uverbs_dev->cdev.dev);
bc38a6ab
RD
968
969err_cdev:
055422dd 970 cdev_del(&uverbs_dev->cdev);
6d6a0e71
AC
971 if (uverbs_dev->devnum < IB_UVERBS_MAX_DEVICES)
972 clear_bit(devnum, dev_map);
973 else
974 clear_bit(devnum, overflow_map);
bc38a6ab
RD
975
976err:
70a30e16 977 kref_put(&uverbs_dev->ref, ib_uverbs_release_dev);
fd60ae40
JM
978 wait_for_completion(&uverbs_dev->comp);
979 kfree(uverbs_dev);
bc38a6ab
RD
980 return;
981}
982
7c1eb45a 983static void ib_uverbs_remove_one(struct ib_device *device, void *client_data)
bc38a6ab 984{
7c1eb45a 985 struct ib_uverbs_device *uverbs_dev = client_data;
bc38a6ab
RD
986
987 if (!uverbs_dev)
988 return;
989
f4e91eb4 990 dev_set_drvdata(uverbs_dev->dev, NULL);
055422dd
AC
991 device_destroy(uverbs_class, uverbs_dev->cdev.dev);
992 cdev_del(&uverbs_dev->cdev);
70a30e16 993
6d6a0e71
AC
994 if (uverbs_dev->devnum < IB_UVERBS_MAX_DEVICES)
995 clear_bit(uverbs_dev->devnum, dev_map);
996 else
997 clear_bit(uverbs_dev->devnum - IB_UVERBS_MAX_DEVICES, overflow_map);
fd60ae40 998
70a30e16 999 kref_put(&uverbs_dev->ref, ib_uverbs_release_dev);
fd60ae40
JM
1000 wait_for_completion(&uverbs_dev->comp);
1001 kfree(uverbs_dev);
bc38a6ab
RD
1002}
1003
2c9ede55 1004static char *uverbs_devnode(struct device *dev, umode_t *mode)
71c29bd5 1005{
b2bc4782
GR
1006 if (mode)
1007 *mode = 0666;
71c29bd5
RD
1008 return kasprintf(GFP_KERNEL, "infiniband/%s", dev_name(dev));
1009}
1010
bc38a6ab
RD
1011static int __init ib_uverbs_init(void)
1012{
1013 int ret;
1014
bc38a6ab
RD
1015 ret = register_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES,
1016 "infiniband_verbs");
1017 if (ret) {
1018 printk(KERN_ERR "user_verbs: couldn't register device number\n");
1019 goto out;
1020 }
1021
70a30e16
RD
1022 uverbs_class = class_create(THIS_MODULE, "infiniband_verbs");
1023 if (IS_ERR(uverbs_class)) {
1024 ret = PTR_ERR(uverbs_class);
bc38a6ab
RD
1025 printk(KERN_ERR "user_verbs: couldn't create class infiniband_verbs\n");
1026 goto out_chrdev;
1027 }
1028
71c29bd5
RD
1029 uverbs_class->devnode = uverbs_devnode;
1030
0933e2d9 1031 ret = class_create_file(uverbs_class, &class_attr_abi_version.attr);
bc38a6ab
RD
1032 if (ret) {
1033 printk(KERN_ERR "user_verbs: couldn't create abi_version attribute\n");
1034 goto out_class;
1035 }
1036
bc38a6ab
RD
1037 ret = ib_register_client(&uverbs_client);
1038 if (ret) {
1039 printk(KERN_ERR "user_verbs: couldn't register client\n");
a265e558 1040 goto out_class;
bc38a6ab
RD
1041 }
1042
1043 return 0;
1044
bc38a6ab 1045out_class:
70a30e16 1046 class_destroy(uverbs_class);
bc38a6ab
RD
1047
1048out_chrdev:
1049 unregister_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES);
1050
1051out:
1052 return ret;
1053}
1054
1055static void __exit ib_uverbs_cleanup(void)
1056{
1057 ib_unregister_client(&uverbs_client);
70a30e16 1058 class_destroy(uverbs_class);
bc38a6ab 1059 unregister_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES);
6d6a0e71
AC
1060 if (overflow_maj)
1061 unregister_chrdev_region(overflow_maj, IB_UVERBS_MAX_DEVICES);
5d7edb3c
RD
1062 idr_destroy(&ib_uverbs_pd_idr);
1063 idr_destroy(&ib_uverbs_mr_idr);
1064 idr_destroy(&ib_uverbs_mw_idr);
1065 idr_destroy(&ib_uverbs_ah_idr);
1066 idr_destroy(&ib_uverbs_cq_idr);
1067 idr_destroy(&ib_uverbs_qp_idr);
1068 idr_destroy(&ib_uverbs_srq_idr);
bc38a6ab
RD
1069}
1070
1071module_init(ib_uverbs_init);
1072module_exit(ib_uverbs_cleanup);