RDMA/core: Simplify destruction of FD uobjects
[sfrench/cifs-2.6.git] / drivers / infiniband / core / uverbs_main.c
1 /*
2  * Copyright (c) 2005 Topspin Communications.  All rights reserved.
3  * Copyright (c) 2005, 2006 Cisco Systems.  All rights reserved.
4  * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
5  * Copyright (c) 2005 Voltaire, Inc. All rights reserved.
6  * Copyright (c) 2005 PathScale, Inc. All rights reserved.
7  *
8  * This software is available to you under a choice of one of two
9  * licenses.  You may choose to be licensed under the terms of the GNU
10  * General Public License (GPL) Version 2, available from the file
11  * COPYING in the main directory of this source tree, or the
12  * OpenIB.org BSD license below:
13  *
14  *     Redistribution and use in source and binary forms, with or
15  *     without modification, are permitted provided that the following
16  *     conditions are met:
17  *
18  *      - Redistributions of source code must retain the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer.
21  *
22  *      - Redistributions in binary form must reproduce the above
23  *        copyright notice, this list of conditions and the following
24  *        disclaimer in the documentation and/or other materials
25  *        provided with the distribution.
26  *
27  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
31  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
32  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
33  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34  * SOFTWARE.
35  */
36
37 #include <linux/module.h>
38 #include <linux/init.h>
39 #include <linux/device.h>
40 #include <linux/err.h>
41 #include <linux/fs.h>
42 #include <linux/poll.h>
43 #include <linux/sched.h>
44 #include <linux/file.h>
45 #include <linux/cdev.h>
46 #include <linux/anon_inodes.h>
47 #include <linux/slab.h>
48 #include <linux/sched/mm.h>
49
50 #include <linux/uaccess.h>
51
52 #include <rdma/ib.h>
53 #include <rdma/uverbs_std_types.h>
54 #include <rdma/rdma_netlink.h>
55
56 #include "uverbs.h"
57 #include "core_priv.h"
58 #include "rdma_core.h"
59
60 MODULE_AUTHOR("Roland Dreier");
61 MODULE_DESCRIPTION("InfiniBand userspace verbs access");
62 MODULE_LICENSE("Dual BSD/GPL");
63
64 enum {
65         IB_UVERBS_MAJOR       = 231,
66         IB_UVERBS_BASE_MINOR  = 192,
67         IB_UVERBS_MAX_DEVICES = RDMA_MAX_PORTS,
68         IB_UVERBS_NUM_FIXED_MINOR = 32,
69         IB_UVERBS_NUM_DYNAMIC_MINOR = IB_UVERBS_MAX_DEVICES - IB_UVERBS_NUM_FIXED_MINOR,
70 };
71
72 #define IB_UVERBS_BASE_DEV      MKDEV(IB_UVERBS_MAJOR, IB_UVERBS_BASE_MINOR)
73
74 static dev_t dynamic_uverbs_dev;
75 static struct class *uverbs_class;
76
77 static DEFINE_IDA(uverbs_ida);
78 static void ib_uverbs_add_one(struct ib_device *device);
79 static void ib_uverbs_remove_one(struct ib_device *device, void *client_data);
80
81 /*
82  * Must be called with the ufile->device->disassociate_srcu held, and the lock
83  * must be held until use of the ucontext is finished.
84  */
85 struct ib_ucontext *ib_uverbs_get_ucontext_file(struct ib_uverbs_file *ufile)
86 {
87         /*
88          * We do not hold the hw_destroy_rwsem lock for this flow, instead
89          * srcu is used. It does not matter if someone races this with
90          * get_context, we get NULL or valid ucontext.
91          */
92         struct ib_ucontext *ucontext = smp_load_acquire(&ufile->ucontext);
93
94         if (!srcu_dereference(ufile->device->ib_dev,
95                               &ufile->device->disassociate_srcu))
96                 return ERR_PTR(-EIO);
97
98         if (!ucontext)
99                 return ERR_PTR(-EINVAL);
100
101         return ucontext;
102 }
103 EXPORT_SYMBOL(ib_uverbs_get_ucontext_file);
104
105 int uverbs_dealloc_mw(struct ib_mw *mw)
106 {
107         struct ib_pd *pd = mw->pd;
108         int ret;
109
110         ret = mw->device->ops.dealloc_mw(mw);
111         if (!ret)
112                 atomic_dec(&pd->usecnt);
113         return ret;
114 }
115
116 static void ib_uverbs_release_dev(struct device *device)
117 {
118         struct ib_uverbs_device *dev =
119                         container_of(device, struct ib_uverbs_device, dev);
120
121         uverbs_destroy_api(dev->uapi);
122         cleanup_srcu_struct(&dev->disassociate_srcu);
123         mutex_destroy(&dev->lists_mutex);
124         mutex_destroy(&dev->xrcd_tree_mutex);
125         kfree(dev);
126 }
127
128 static void ib_uverbs_release_async_event_file(struct kref *ref)
129 {
130         struct ib_uverbs_async_event_file *file =
131                 container_of(ref, struct ib_uverbs_async_event_file, ref);
132
133         kfree(file);
134 }
135
136 void ib_uverbs_release_ucq(struct ib_uverbs_file *file,
137                           struct ib_uverbs_completion_event_file *ev_file,
138                           struct ib_ucq_object *uobj)
139 {
140         struct ib_uverbs_event *evt, *tmp;
141
142         if (ev_file) {
143                 spin_lock_irq(&ev_file->ev_queue.lock);
144                 list_for_each_entry_safe(evt, tmp, &uobj->comp_list, obj_list) {
145                         list_del(&evt->list);
146                         kfree(evt);
147                 }
148                 spin_unlock_irq(&ev_file->ev_queue.lock);
149
150                 uverbs_uobject_put(&ev_file->uobj);
151         }
152
153         spin_lock_irq(&file->async_file->ev_queue.lock);
154         list_for_each_entry_safe(evt, tmp, &uobj->async_list, obj_list) {
155                 list_del(&evt->list);
156                 kfree(evt);
157         }
158         spin_unlock_irq(&file->async_file->ev_queue.lock);
159 }
160
161 void ib_uverbs_release_uevent(struct ib_uverbs_file *file,
162                               struct ib_uevent_object *uobj)
163 {
164         struct ib_uverbs_event *evt, *tmp;
165
166         spin_lock_irq(&file->async_file->ev_queue.lock);
167         list_for_each_entry_safe(evt, tmp, &uobj->event_list, obj_list) {
168                 list_del(&evt->list);
169                 kfree(evt);
170         }
171         spin_unlock_irq(&file->async_file->ev_queue.lock);
172 }
173
174 void ib_uverbs_detach_umcast(struct ib_qp *qp,
175                              struct ib_uqp_object *uobj)
176 {
177         struct ib_uverbs_mcast_entry *mcast, *tmp;
178
179         list_for_each_entry_safe(mcast, tmp, &uobj->mcast_list, list) {
180                 ib_detach_mcast(qp, &mcast->gid, mcast->lid);
181                 list_del(&mcast->list);
182                 kfree(mcast);
183         }
184 }
185
186 static void ib_uverbs_comp_dev(struct ib_uverbs_device *dev)
187 {
188         complete(&dev->comp);
189 }
190
191 void ib_uverbs_release_file(struct kref *ref)
192 {
193         struct ib_uverbs_file *file =
194                 container_of(ref, struct ib_uverbs_file, ref);
195         struct ib_device *ib_dev;
196         int srcu_key;
197
198         release_ufile_idr_uobject(file);
199
200         srcu_key = srcu_read_lock(&file->device->disassociate_srcu);
201         ib_dev = srcu_dereference(file->device->ib_dev,
202                                   &file->device->disassociate_srcu);
203         if (ib_dev && !ib_dev->ops.disassociate_ucontext)
204                 module_put(ib_dev->ops.owner);
205         srcu_read_unlock(&file->device->disassociate_srcu, srcu_key);
206
207         if (atomic_dec_and_test(&file->device->refcount))
208                 ib_uverbs_comp_dev(file->device);
209
210         if (file->async_file)
211                 kref_put(&file->async_file->ref,
212                          ib_uverbs_release_async_event_file);
213         put_device(&file->device->dev);
214
215         if (file->disassociate_page)
216                 __free_pages(file->disassociate_page, 0);
217         mutex_destroy(&file->umap_lock);
218         mutex_destroy(&file->ucontext_lock);
219         kfree(file);
220 }
221
222 static ssize_t ib_uverbs_event_read(struct ib_uverbs_event_queue *ev_queue,
223                                     struct file *filp, char __user *buf,
224                                     size_t count, loff_t *pos,
225                                     size_t eventsz)
226 {
227         struct ib_uverbs_event *event;
228         int ret = 0;
229
230         spin_lock_irq(&ev_queue->lock);
231
232         while (list_empty(&ev_queue->event_list)) {
233                 spin_unlock_irq(&ev_queue->lock);
234
235                 if (filp->f_flags & O_NONBLOCK)
236                         return -EAGAIN;
237
238                 if (wait_event_interruptible(ev_queue->poll_wait,
239                                              (!list_empty(&ev_queue->event_list) ||
240                                               ev_queue->is_closed)))
241                         return -ERESTARTSYS;
242
243                 spin_lock_irq(&ev_queue->lock);
244
245                 /* If device was disassociated and no event exists set an error */
246                 if (list_empty(&ev_queue->event_list) && ev_queue->is_closed) {
247                         spin_unlock_irq(&ev_queue->lock);
248                         return -EIO;
249                 }
250         }
251
252         event = list_entry(ev_queue->event_list.next, struct ib_uverbs_event, list);
253
254         if (eventsz > count) {
255                 ret   = -EINVAL;
256                 event = NULL;
257         } else {
258                 list_del(ev_queue->event_list.next);
259                 if (event->counter) {
260                         ++(*event->counter);
261                         list_del(&event->obj_list);
262                 }
263         }
264
265         spin_unlock_irq(&ev_queue->lock);
266
267         if (event) {
268                 if (copy_to_user(buf, event, eventsz))
269                         ret = -EFAULT;
270                 else
271                         ret = eventsz;
272         }
273
274         kfree(event);
275
276         return ret;
277 }
278
279 static ssize_t ib_uverbs_async_event_read(struct file *filp, char __user *buf,
280                                           size_t count, loff_t *pos)
281 {
282         struct ib_uverbs_async_event_file *file = filp->private_data;
283
284         return ib_uverbs_event_read(&file->ev_queue, filp, buf, count, pos,
285                                     sizeof(struct ib_uverbs_async_event_desc));
286 }
287
288 static ssize_t ib_uverbs_comp_event_read(struct file *filp, char __user *buf,
289                                          size_t count, loff_t *pos)
290 {
291         struct ib_uverbs_completion_event_file *comp_ev_file =
292                 filp->private_data;
293
294         return ib_uverbs_event_read(&comp_ev_file->ev_queue, filp, buf, count,
295                                     pos,
296                                     sizeof(struct ib_uverbs_comp_event_desc));
297 }
298
299 static __poll_t ib_uverbs_event_poll(struct ib_uverbs_event_queue *ev_queue,
300                                          struct file *filp,
301                                          struct poll_table_struct *wait)
302 {
303         __poll_t pollflags = 0;
304
305         poll_wait(filp, &ev_queue->poll_wait, wait);
306
307         spin_lock_irq(&ev_queue->lock);
308         if (!list_empty(&ev_queue->event_list))
309                 pollflags = EPOLLIN | EPOLLRDNORM;
310         spin_unlock_irq(&ev_queue->lock);
311
312         return pollflags;
313 }
314
315 static __poll_t ib_uverbs_async_event_poll(struct file *filp,
316                                                struct poll_table_struct *wait)
317 {
318         struct ib_uverbs_async_event_file *file = filp->private_data;
319
320         return ib_uverbs_event_poll(&file->ev_queue, filp, wait);
321 }
322
323 static __poll_t ib_uverbs_comp_event_poll(struct file *filp,
324                                               struct poll_table_struct *wait)
325 {
326         struct ib_uverbs_completion_event_file *comp_ev_file =
327                 filp->private_data;
328
329         return ib_uverbs_event_poll(&comp_ev_file->ev_queue, filp, wait);
330 }
331
332 static int ib_uverbs_async_event_fasync(int fd, struct file *filp, int on)
333 {
334         struct ib_uverbs_async_event_file *file = filp->private_data;
335
336         return fasync_helper(fd, filp, on, &file->ev_queue.async_queue);
337 }
338
339 static int ib_uverbs_comp_event_fasync(int fd, struct file *filp, int on)
340 {
341         struct ib_uverbs_completion_event_file *comp_ev_file =
342                 filp->private_data;
343
344         return fasync_helper(fd, filp, on, &comp_ev_file->ev_queue.async_queue);
345 }
346
347 static int ib_uverbs_async_event_close(struct inode *inode, struct file *filp)
348 {
349         struct ib_uverbs_async_event_file *file = filp->private_data;
350         struct ib_uverbs_file *uverbs_file = file->uverbs_file;
351         struct ib_uverbs_event *entry, *tmp;
352         int closed_already = 0;
353
354         mutex_lock(&uverbs_file->device->lists_mutex);
355         spin_lock_irq(&file->ev_queue.lock);
356         closed_already = file->ev_queue.is_closed;
357         file->ev_queue.is_closed = 1;
358         list_for_each_entry_safe(entry, tmp, &file->ev_queue.event_list, list) {
359                 if (entry->counter)
360                         list_del(&entry->obj_list);
361                 kfree(entry);
362         }
363         spin_unlock_irq(&file->ev_queue.lock);
364         if (!closed_already) {
365                 list_del(&file->list);
366                 ib_unregister_event_handler(&uverbs_file->event_handler);
367         }
368         mutex_unlock(&uverbs_file->device->lists_mutex);
369
370         kref_put(&uverbs_file->ref, ib_uverbs_release_file);
371         kref_put(&file->ref, ib_uverbs_release_async_event_file);
372
373         return 0;
374 }
375
376 const struct file_operations uverbs_event_fops = {
377         .owner   = THIS_MODULE,
378         .read    = ib_uverbs_comp_event_read,
379         .poll    = ib_uverbs_comp_event_poll,
380         .release = uverbs_uobject_fd_release,
381         .fasync  = ib_uverbs_comp_event_fasync,
382         .llseek  = no_llseek,
383 };
384
385 static const struct file_operations uverbs_async_event_fops = {
386         .owner   = THIS_MODULE,
387         .read    = ib_uverbs_async_event_read,
388         .poll    = ib_uverbs_async_event_poll,
389         .release = ib_uverbs_async_event_close,
390         .fasync  = ib_uverbs_async_event_fasync,
391         .llseek  = no_llseek,
392 };
393
394 void ib_uverbs_comp_handler(struct ib_cq *cq, void *cq_context)
395 {
396         struct ib_uverbs_event_queue   *ev_queue = cq_context;
397         struct ib_ucq_object           *uobj;
398         struct ib_uverbs_event         *entry;
399         unsigned long                   flags;
400
401         if (!ev_queue)
402                 return;
403
404         spin_lock_irqsave(&ev_queue->lock, flags);
405         if (ev_queue->is_closed) {
406                 spin_unlock_irqrestore(&ev_queue->lock, flags);
407                 return;
408         }
409
410         entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
411         if (!entry) {
412                 spin_unlock_irqrestore(&ev_queue->lock, flags);
413                 return;
414         }
415
416         uobj = container_of(cq->uobject, struct ib_ucq_object, uobject);
417
418         entry->desc.comp.cq_handle = cq->uobject->user_handle;
419         entry->counter             = &uobj->comp_events_reported;
420
421         list_add_tail(&entry->list, &ev_queue->event_list);
422         list_add_tail(&entry->obj_list, &uobj->comp_list);
423         spin_unlock_irqrestore(&ev_queue->lock, flags);
424
425         wake_up_interruptible(&ev_queue->poll_wait);
426         kill_fasync(&ev_queue->async_queue, SIGIO, POLL_IN);
427 }
428
429 static void ib_uverbs_async_handler(struct ib_uverbs_file *file,
430                                     __u64 element, __u64 event,
431                                     struct list_head *obj_list,
432                                     u32 *counter)
433 {
434         struct ib_uverbs_event *entry;
435         unsigned long flags;
436
437         spin_lock_irqsave(&file->async_file->ev_queue.lock, flags);
438         if (file->async_file->ev_queue.is_closed) {
439                 spin_unlock_irqrestore(&file->async_file->ev_queue.lock, flags);
440                 return;
441         }
442
443         entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
444         if (!entry) {
445                 spin_unlock_irqrestore(&file->async_file->ev_queue.lock, flags);
446                 return;
447         }
448
449         entry->desc.async.element    = element;
450         entry->desc.async.event_type = event;
451         entry->desc.async.reserved   = 0;
452         entry->counter               = counter;
453
454         list_add_tail(&entry->list, &file->async_file->ev_queue.event_list);
455         if (obj_list)
456                 list_add_tail(&entry->obj_list, obj_list);
457         spin_unlock_irqrestore(&file->async_file->ev_queue.lock, flags);
458
459         wake_up_interruptible(&file->async_file->ev_queue.poll_wait);
460         kill_fasync(&file->async_file->ev_queue.async_queue, SIGIO, POLL_IN);
461 }
462
463 void ib_uverbs_cq_event_handler(struct ib_event *event, void *context_ptr)
464 {
465         struct ib_ucq_object *uobj = container_of(event->element.cq->uobject,
466                                                   struct ib_ucq_object, uobject);
467
468         ib_uverbs_async_handler(uobj->uobject.ufile, uobj->uobject.user_handle,
469                                 event->event, &uobj->async_list,
470                                 &uobj->async_events_reported);
471 }
472
473 void ib_uverbs_qp_event_handler(struct ib_event *event, void *context_ptr)
474 {
475         struct ib_uevent_object *uobj;
476
477         /* for XRC target qp's, check that qp is live */
478         if (!event->element.qp->uobject)
479                 return;
480
481         uobj = container_of(event->element.qp->uobject,
482                             struct ib_uevent_object, uobject);
483
484         ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle,
485                                 event->event, &uobj->event_list,
486                                 &uobj->events_reported);
487 }
488
489 void ib_uverbs_wq_event_handler(struct ib_event *event, void *context_ptr)
490 {
491         struct ib_uevent_object *uobj = container_of(event->element.wq->uobject,
492                                                   struct ib_uevent_object, uobject);
493
494         ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle,
495                                 event->event, &uobj->event_list,
496                                 &uobj->events_reported);
497 }
498
499 void ib_uverbs_srq_event_handler(struct ib_event *event, void *context_ptr)
500 {
501         struct ib_uevent_object *uobj;
502
503         uobj = container_of(event->element.srq->uobject,
504                             struct ib_uevent_object, uobject);
505
506         ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle,
507                                 event->event, &uobj->event_list,
508                                 &uobj->events_reported);
509 }
510
511 void ib_uverbs_event_handler(struct ib_event_handler *handler,
512                              struct ib_event *event)
513 {
514         struct ib_uverbs_file *file =
515                 container_of(handler, struct ib_uverbs_file, event_handler);
516
517         ib_uverbs_async_handler(file, event->element.port_num, event->event,
518                                 NULL, NULL);
519 }
520
521 void ib_uverbs_free_async_event_file(struct ib_uverbs_file *file)
522 {
523         kref_put(&file->async_file->ref, ib_uverbs_release_async_event_file);
524         file->async_file = NULL;
525 }
526
527 void ib_uverbs_init_event_queue(struct ib_uverbs_event_queue *ev_queue)
528 {
529         spin_lock_init(&ev_queue->lock);
530         INIT_LIST_HEAD(&ev_queue->event_list);
531         init_waitqueue_head(&ev_queue->poll_wait);
532         ev_queue->is_closed   = 0;
533         ev_queue->async_queue = NULL;
534 }
535
536 struct file *ib_uverbs_alloc_async_event_file(struct ib_uverbs_file *uverbs_file,
537                                               struct ib_device  *ib_dev)
538 {
539         struct ib_uverbs_async_event_file *ev_file;
540         struct file *filp;
541
542         ev_file = kzalloc(sizeof(*ev_file), GFP_KERNEL);
543         if (!ev_file)
544                 return ERR_PTR(-ENOMEM);
545
546         ib_uverbs_init_event_queue(&ev_file->ev_queue);
547         ev_file->uverbs_file = uverbs_file;
548         kref_get(&ev_file->uverbs_file->ref);
549         kref_init(&ev_file->ref);
550         filp = anon_inode_getfile("[infinibandevent]", &uverbs_async_event_fops,
551                                   ev_file, O_RDONLY);
552         if (IS_ERR(filp))
553                 goto err_put_refs;
554
555         mutex_lock(&uverbs_file->device->lists_mutex);
556         list_add_tail(&ev_file->list,
557                       &uverbs_file->device->uverbs_events_file_list);
558         mutex_unlock(&uverbs_file->device->lists_mutex);
559
560         WARN_ON(uverbs_file->async_file);
561         uverbs_file->async_file = ev_file;
562         kref_get(&uverbs_file->async_file->ref);
563         INIT_IB_EVENT_HANDLER(&uverbs_file->event_handler,
564                               ib_dev,
565                               ib_uverbs_event_handler);
566         ib_register_event_handler(&uverbs_file->event_handler);
567         /* At that point async file stuff was fully set */
568
569         return filp;
570
571 err_put_refs:
572         kref_put(&ev_file->uverbs_file->ref, ib_uverbs_release_file);
573         kref_put(&ev_file->ref, ib_uverbs_release_async_event_file);
574         return filp;
575 }
576
577 static ssize_t verify_hdr(struct ib_uverbs_cmd_hdr *hdr,
578                           struct ib_uverbs_ex_cmd_hdr *ex_hdr, size_t count,
579                           const struct uverbs_api_write_method *method_elm)
580 {
581         if (method_elm->is_ex) {
582                 count -= sizeof(*hdr) + sizeof(*ex_hdr);
583
584                 if ((hdr->in_words + ex_hdr->provider_in_words) * 8 != count)
585                         return -EINVAL;
586
587                 if (hdr->in_words * 8 < method_elm->req_size)
588                         return -ENOSPC;
589
590                 if (ex_hdr->cmd_hdr_reserved)
591                         return -EINVAL;
592
593                 if (ex_hdr->response) {
594                         if (!hdr->out_words && !ex_hdr->provider_out_words)
595                                 return -EINVAL;
596
597                         if (hdr->out_words * 8 < method_elm->resp_size)
598                                 return -ENOSPC;
599
600                         if (!access_ok(u64_to_user_ptr(ex_hdr->response),
601                                        (hdr->out_words + ex_hdr->provider_out_words) * 8))
602                                 return -EFAULT;
603                 } else {
604                         if (hdr->out_words || ex_hdr->provider_out_words)
605                                 return -EINVAL;
606                 }
607
608                 return 0;
609         }
610
611         /* not extended command */
612         if (hdr->in_words * 4 != count)
613                 return -EINVAL;
614
615         if (count < method_elm->req_size + sizeof(hdr)) {
616                 /*
617                  * rdma-core v18 and v19 have a bug where they send DESTROY_CQ
618                  * with a 16 byte write instead of 24. Old kernels didn't
619                  * check the size so they allowed this. Now that the size is
620                  * checked provide a compatibility work around to not break
621                  * those userspaces.
622                  */
623                 if (hdr->command == IB_USER_VERBS_CMD_DESTROY_CQ &&
624                     count == 16) {
625                         hdr->in_words = 6;
626                         return 0;
627                 }
628                 return -ENOSPC;
629         }
630         if (hdr->out_words * 4 < method_elm->resp_size)
631                 return -ENOSPC;
632
633         return 0;
634 }
635
636 static ssize_t ib_uverbs_write(struct file *filp, const char __user *buf,
637                              size_t count, loff_t *pos)
638 {
639         struct ib_uverbs_file *file = filp->private_data;
640         const struct uverbs_api_write_method *method_elm;
641         struct uverbs_api *uapi = file->device->uapi;
642         struct ib_uverbs_ex_cmd_hdr ex_hdr;
643         struct ib_uverbs_cmd_hdr hdr;
644         struct uverbs_attr_bundle bundle;
645         int srcu_key;
646         ssize_t ret;
647
648         if (!ib_safe_file_access(filp)) {
649                 pr_err_once("uverbs_write: process %d (%s) changed security contexts after opening file descriptor, this is not allowed.\n",
650                             task_tgid_vnr(current), current->comm);
651                 return -EACCES;
652         }
653
654         if (count < sizeof(hdr))
655                 return -EINVAL;
656
657         if (copy_from_user(&hdr, buf, sizeof(hdr)))
658                 return -EFAULT;
659
660         method_elm = uapi_get_method(uapi, hdr.command);
661         if (IS_ERR(method_elm))
662                 return PTR_ERR(method_elm);
663
664         if (method_elm->is_ex) {
665                 if (count < (sizeof(hdr) + sizeof(ex_hdr)))
666                         return -EINVAL;
667                 if (copy_from_user(&ex_hdr, buf + sizeof(hdr), sizeof(ex_hdr)))
668                         return -EFAULT;
669         }
670
671         ret = verify_hdr(&hdr, &ex_hdr, count, method_elm);
672         if (ret)
673                 return ret;
674
675         srcu_key = srcu_read_lock(&file->device->disassociate_srcu);
676
677         buf += sizeof(hdr);
678
679         memset(bundle.attr_present, 0, sizeof(bundle.attr_present));
680         bundle.ufile = file;
681         bundle.context = NULL; /* only valid if bundle has uobject */
682         if (!method_elm->is_ex) {
683                 size_t in_len = hdr.in_words * 4 - sizeof(hdr);
684                 size_t out_len = hdr.out_words * 4;
685                 u64 response = 0;
686
687                 if (method_elm->has_udata) {
688                         bundle.driver_udata.inlen =
689                                 in_len - method_elm->req_size;
690                         in_len = method_elm->req_size;
691                         if (bundle.driver_udata.inlen)
692                                 bundle.driver_udata.inbuf = buf + in_len;
693                         else
694                                 bundle.driver_udata.inbuf = NULL;
695                 } else {
696                         memset(&bundle.driver_udata, 0,
697                                sizeof(bundle.driver_udata));
698                 }
699
700                 if (method_elm->has_resp) {
701                         /*
702                          * The macros check that if has_resp is set
703                          * then the command request structure starts
704                          * with a '__aligned u64 response' member.
705                          */
706                         ret = get_user(response, (const u64 __user *)buf);
707                         if (ret)
708                                 goto out_unlock;
709
710                         if (method_elm->has_udata) {
711                                 bundle.driver_udata.outlen =
712                                         out_len - method_elm->resp_size;
713                                 out_len = method_elm->resp_size;
714                                 if (bundle.driver_udata.outlen)
715                                         bundle.driver_udata.outbuf =
716                                                 u64_to_user_ptr(response +
717                                                                 out_len);
718                                 else
719                                         bundle.driver_udata.outbuf = NULL;
720                         }
721                 } else {
722                         bundle.driver_udata.outlen = 0;
723                         bundle.driver_udata.outbuf = NULL;
724                 }
725
726                 ib_uverbs_init_udata_buf_or_null(
727                         &bundle.ucore, buf, u64_to_user_ptr(response),
728                         in_len, out_len);
729         } else {
730                 buf += sizeof(ex_hdr);
731
732                 ib_uverbs_init_udata_buf_or_null(&bundle.ucore, buf,
733                                         u64_to_user_ptr(ex_hdr.response),
734                                         hdr.in_words * 8, hdr.out_words * 8);
735
736                 ib_uverbs_init_udata_buf_or_null(
737                         &bundle.driver_udata, buf + bundle.ucore.inlen,
738                         u64_to_user_ptr(ex_hdr.response) + bundle.ucore.outlen,
739                         ex_hdr.provider_in_words * 8,
740                         ex_hdr.provider_out_words * 8);
741
742         }
743
744         ret = method_elm->handler(&bundle);
745 out_unlock:
746         srcu_read_unlock(&file->device->disassociate_srcu, srcu_key);
747         return (ret) ? : count;
748 }
749
750 static const struct vm_operations_struct rdma_umap_ops;
751
752 static int ib_uverbs_mmap(struct file *filp, struct vm_area_struct *vma)
753 {
754         struct ib_uverbs_file *file = filp->private_data;
755         struct ib_ucontext *ucontext;
756         int ret = 0;
757         int srcu_key;
758
759         srcu_key = srcu_read_lock(&file->device->disassociate_srcu);
760         ucontext = ib_uverbs_get_ucontext_file(file);
761         if (IS_ERR(ucontext)) {
762                 ret = PTR_ERR(ucontext);
763                 goto out;
764         }
765         vma->vm_ops = &rdma_umap_ops;
766         ret = ucontext->device->ops.mmap(ucontext, vma);
767 out:
768         srcu_read_unlock(&file->device->disassociate_srcu, srcu_key);
769         return ret;
770 }
771
772 /*
773  * The VMA has been dup'd, initialize the vm_private_data with a new tracking
774  * struct
775  */
776 static void rdma_umap_open(struct vm_area_struct *vma)
777 {
778         struct ib_uverbs_file *ufile = vma->vm_file->private_data;
779         struct rdma_umap_priv *opriv = vma->vm_private_data;
780         struct rdma_umap_priv *priv;
781
782         if (!opriv)
783                 return;
784
785         /* We are racing with disassociation */
786         if (!down_read_trylock(&ufile->hw_destroy_rwsem))
787                 goto out_zap;
788         /*
789          * Disassociation already completed, the VMA should already be zapped.
790          */
791         if (!ufile->ucontext)
792                 goto out_unlock;
793
794         priv = kzalloc(sizeof(*priv), GFP_KERNEL);
795         if (!priv)
796                 goto out_unlock;
797         rdma_umap_priv_init(priv, vma, opriv->entry);
798
799         up_read(&ufile->hw_destroy_rwsem);
800         return;
801
802 out_unlock:
803         up_read(&ufile->hw_destroy_rwsem);
804 out_zap:
805         /*
806          * We can't allow the VMA to be created with the actual IO pages, that
807          * would break our API contract, and it can't be stopped at this
808          * point, so zap it.
809          */
810         vma->vm_private_data = NULL;
811         zap_vma_ptes(vma, vma->vm_start, vma->vm_end - vma->vm_start);
812 }
813
814 static void rdma_umap_close(struct vm_area_struct *vma)
815 {
816         struct ib_uverbs_file *ufile = vma->vm_file->private_data;
817         struct rdma_umap_priv *priv = vma->vm_private_data;
818
819         if (!priv)
820                 return;
821
822         /*
823          * The vma holds a reference on the struct file that created it, which
824          * in turn means that the ib_uverbs_file is guaranteed to exist at
825          * this point.
826          */
827         mutex_lock(&ufile->umap_lock);
828         if (priv->entry)
829                 rdma_user_mmap_entry_put(priv->entry);
830
831         list_del(&priv->list);
832         mutex_unlock(&ufile->umap_lock);
833         kfree(priv);
834 }
835
836 /*
837  * Once the zap_vma_ptes has been called touches to the VMA will come here and
838  * we return a dummy writable zero page for all the pfns.
839  */
840 static vm_fault_t rdma_umap_fault(struct vm_fault *vmf)
841 {
842         struct ib_uverbs_file *ufile = vmf->vma->vm_file->private_data;
843         struct rdma_umap_priv *priv = vmf->vma->vm_private_data;
844         vm_fault_t ret = 0;
845
846         if (!priv)
847                 return VM_FAULT_SIGBUS;
848
849         /* Read only pages can just use the system zero page. */
850         if (!(vmf->vma->vm_flags & (VM_WRITE | VM_MAYWRITE))) {
851                 vmf->page = ZERO_PAGE(vmf->address);
852                 get_page(vmf->page);
853                 return 0;
854         }
855
856         mutex_lock(&ufile->umap_lock);
857         if (!ufile->disassociate_page)
858                 ufile->disassociate_page =
859                         alloc_pages(vmf->gfp_mask | __GFP_ZERO, 0);
860
861         if (ufile->disassociate_page) {
862                 /*
863                  * This VMA is forced to always be shared so this doesn't have
864                  * to worry about COW.
865                  */
866                 vmf->page = ufile->disassociate_page;
867                 get_page(vmf->page);
868         } else {
869                 ret = VM_FAULT_SIGBUS;
870         }
871         mutex_unlock(&ufile->umap_lock);
872
873         return ret;
874 }
875
876 static const struct vm_operations_struct rdma_umap_ops = {
877         .open = rdma_umap_open,
878         .close = rdma_umap_close,
879         .fault = rdma_umap_fault,
880 };
881
882 void uverbs_user_mmap_disassociate(struct ib_uverbs_file *ufile)
883 {
884         struct rdma_umap_priv *priv, *next_priv;
885
886         lockdep_assert_held(&ufile->hw_destroy_rwsem);
887
888         while (1) {
889                 struct mm_struct *mm = NULL;
890
891                 /* Get an arbitrary mm pointer that hasn't been cleaned yet */
892                 mutex_lock(&ufile->umap_lock);
893                 while (!list_empty(&ufile->umaps)) {
894                         int ret;
895
896                         priv = list_first_entry(&ufile->umaps,
897                                                 struct rdma_umap_priv, list);
898                         mm = priv->vma->vm_mm;
899                         ret = mmget_not_zero(mm);
900                         if (!ret) {
901                                 list_del_init(&priv->list);
902                                 mm = NULL;
903                                 continue;
904                         }
905                         break;
906                 }
907                 mutex_unlock(&ufile->umap_lock);
908                 if (!mm)
909                         return;
910
911                 /*
912                  * The umap_lock is nested under mmap_sem since it used within
913                  * the vma_ops callbacks, so we have to clean the list one mm
914                  * at a time to get the lock ordering right. Typically there
915                  * will only be one mm, so no big deal.
916                  */
917                 down_read(&mm->mmap_sem);
918                 if (!mmget_still_valid(mm))
919                         goto skip_mm;
920                 mutex_lock(&ufile->umap_lock);
921                 list_for_each_entry_safe (priv, next_priv, &ufile->umaps,
922                                           list) {
923                         struct vm_area_struct *vma = priv->vma;
924
925                         if (vma->vm_mm != mm)
926                                 continue;
927                         list_del_init(&priv->list);
928
929                         zap_vma_ptes(vma, vma->vm_start,
930                                      vma->vm_end - vma->vm_start);
931
932                         if (priv->entry) {
933                                 rdma_user_mmap_entry_put(priv->entry);
934                                 priv->entry = NULL;
935                         }
936                 }
937                 mutex_unlock(&ufile->umap_lock);
938         skip_mm:
939                 up_read(&mm->mmap_sem);
940                 mmput(mm);
941         }
942 }
943
944 /*
945  * ib_uverbs_open() does not need the BKL:
946  *
947  *  - the ib_uverbs_device structures are properly reference counted and
948  *    everything else is purely local to the file being created, so
949  *    races against other open calls are not a problem;
950  *  - there is no ioctl method to race against;
951  *  - the open method will either immediately run -ENXIO, or all
952  *    required initialization will be done.
953  */
954 static int ib_uverbs_open(struct inode *inode, struct file *filp)
955 {
956         struct ib_uverbs_device *dev;
957         struct ib_uverbs_file *file;
958         struct ib_device *ib_dev;
959         int ret;
960         int module_dependent;
961         int srcu_key;
962
963         dev = container_of(inode->i_cdev, struct ib_uverbs_device, cdev);
964         if (!atomic_inc_not_zero(&dev->refcount))
965                 return -ENXIO;
966
967         get_device(&dev->dev);
968         srcu_key = srcu_read_lock(&dev->disassociate_srcu);
969         mutex_lock(&dev->lists_mutex);
970         ib_dev = srcu_dereference(dev->ib_dev,
971                                   &dev->disassociate_srcu);
972         if (!ib_dev) {
973                 ret = -EIO;
974                 goto err;
975         }
976
977         if (!rdma_dev_access_netns(ib_dev, current->nsproxy->net_ns)) {
978                 ret = -EPERM;
979                 goto err;
980         }
981
982         /* In case IB device supports disassociate ucontext, there is no hard
983          * dependency between uverbs device and its low level device.
984          */
985         module_dependent = !(ib_dev->ops.disassociate_ucontext);
986
987         if (module_dependent) {
988                 if (!try_module_get(ib_dev->ops.owner)) {
989                         ret = -ENODEV;
990                         goto err;
991                 }
992         }
993
994         file = kzalloc(sizeof(*file), GFP_KERNEL);
995         if (!file) {
996                 ret = -ENOMEM;
997                 if (module_dependent)
998                         goto err_module;
999
1000                 goto err;
1001         }
1002
1003         file->device     = dev;
1004         kref_init(&file->ref);
1005         mutex_init(&file->ucontext_lock);
1006
1007         spin_lock_init(&file->uobjects_lock);
1008         INIT_LIST_HEAD(&file->uobjects);
1009         init_rwsem(&file->hw_destroy_rwsem);
1010         mutex_init(&file->umap_lock);
1011         INIT_LIST_HEAD(&file->umaps);
1012
1013         filp->private_data = file;
1014         list_add_tail(&file->list, &dev->uverbs_file_list);
1015         mutex_unlock(&dev->lists_mutex);
1016         srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
1017
1018         setup_ufile_idr_uobject(file);
1019
1020         return stream_open(inode, filp);
1021
1022 err_module:
1023         module_put(ib_dev->ops.owner);
1024
1025 err:
1026         mutex_unlock(&dev->lists_mutex);
1027         srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
1028         if (atomic_dec_and_test(&dev->refcount))
1029                 ib_uverbs_comp_dev(dev);
1030
1031         put_device(&dev->dev);
1032         return ret;
1033 }
1034
1035 static int ib_uverbs_close(struct inode *inode, struct file *filp)
1036 {
1037         struct ib_uverbs_file *file = filp->private_data;
1038
1039         uverbs_destroy_ufile_hw(file, RDMA_REMOVE_CLOSE);
1040
1041         mutex_lock(&file->device->lists_mutex);
1042         list_del_init(&file->list);
1043         mutex_unlock(&file->device->lists_mutex);
1044
1045         kref_put(&file->ref, ib_uverbs_release_file);
1046
1047         return 0;
1048 }
1049
1050 static const struct file_operations uverbs_fops = {
1051         .owner   = THIS_MODULE,
1052         .write   = ib_uverbs_write,
1053         .open    = ib_uverbs_open,
1054         .release = ib_uverbs_close,
1055         .llseek  = no_llseek,
1056         .unlocked_ioctl = ib_uverbs_ioctl,
1057         .compat_ioctl = compat_ptr_ioctl,
1058 };
1059
1060 static const struct file_operations uverbs_mmap_fops = {
1061         .owner   = THIS_MODULE,
1062         .write   = ib_uverbs_write,
1063         .mmap    = ib_uverbs_mmap,
1064         .open    = ib_uverbs_open,
1065         .release = ib_uverbs_close,
1066         .llseek  = no_llseek,
1067         .unlocked_ioctl = ib_uverbs_ioctl,
1068         .compat_ioctl = compat_ptr_ioctl,
1069 };
1070
1071 static int ib_uverbs_get_nl_info(struct ib_device *ibdev, void *client_data,
1072                                  struct ib_client_nl_info *res)
1073 {
1074         struct ib_uverbs_device *uverbs_dev = client_data;
1075         int ret;
1076
1077         if (res->port != -1)
1078                 return -EINVAL;
1079
1080         res->abi = ibdev->ops.uverbs_abi_ver;
1081         res->cdev = &uverbs_dev->dev;
1082
1083         /*
1084          * To support DRIVER_ID binding in userspace some of the driver need
1085          * upgrading to expose their PCI dependent revision information
1086          * through get_context instead of relying on modalias matching. When
1087          * the drivers are fixed they can drop this flag.
1088          */
1089         if (!ibdev->ops.uverbs_no_driver_id_binding) {
1090                 ret = nla_put_u32(res->nl_msg, RDMA_NLDEV_ATTR_UVERBS_DRIVER_ID,
1091                                   ibdev->ops.driver_id);
1092                 if (ret)
1093                         return ret;
1094         }
1095         return 0;
1096 }
1097
1098 static struct ib_client uverbs_client = {
1099         .name   = "uverbs",
1100         .no_kverbs_req = true,
1101         .add    = ib_uverbs_add_one,
1102         .remove = ib_uverbs_remove_one,
1103         .get_nl_info = ib_uverbs_get_nl_info,
1104 };
1105 MODULE_ALIAS_RDMA_CLIENT("uverbs");
1106
1107 static ssize_t ibdev_show(struct device *device, struct device_attribute *attr,
1108                           char *buf)
1109 {
1110         struct ib_uverbs_device *dev =
1111                         container_of(device, struct ib_uverbs_device, dev);
1112         int ret = -ENODEV;
1113         int srcu_key;
1114         struct ib_device *ib_dev;
1115
1116         srcu_key = srcu_read_lock(&dev->disassociate_srcu);
1117         ib_dev = srcu_dereference(dev->ib_dev, &dev->disassociate_srcu);
1118         if (ib_dev)
1119                 ret = sprintf(buf, "%s\n", dev_name(&ib_dev->dev));
1120         srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
1121
1122         return ret;
1123 }
1124 static DEVICE_ATTR_RO(ibdev);
1125
1126 static ssize_t abi_version_show(struct device *device,
1127                                 struct device_attribute *attr, char *buf)
1128 {
1129         struct ib_uverbs_device *dev =
1130                         container_of(device, struct ib_uverbs_device, dev);
1131         int ret = -ENODEV;
1132         int srcu_key;
1133         struct ib_device *ib_dev;
1134
1135         srcu_key = srcu_read_lock(&dev->disassociate_srcu);
1136         ib_dev = srcu_dereference(dev->ib_dev, &dev->disassociate_srcu);
1137         if (ib_dev)
1138                 ret = sprintf(buf, "%u\n", ib_dev->ops.uverbs_abi_ver);
1139         srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
1140
1141         return ret;
1142 }
1143 static DEVICE_ATTR_RO(abi_version);
1144
1145 static struct attribute *ib_dev_attrs[] = {
1146         &dev_attr_abi_version.attr,
1147         &dev_attr_ibdev.attr,
1148         NULL,
1149 };
1150
1151 static const struct attribute_group dev_attr_group = {
1152         .attrs = ib_dev_attrs,
1153 };
1154
1155 static CLASS_ATTR_STRING(abi_version, S_IRUGO,
1156                          __stringify(IB_USER_VERBS_ABI_VERSION));
1157
1158 static int ib_uverbs_create_uapi(struct ib_device *device,
1159                                  struct ib_uverbs_device *uverbs_dev)
1160 {
1161         struct uverbs_api *uapi;
1162
1163         uapi = uverbs_alloc_api(device);
1164         if (IS_ERR(uapi))
1165                 return PTR_ERR(uapi);
1166
1167         uverbs_dev->uapi = uapi;
1168         return 0;
1169 }
1170
1171 static void ib_uverbs_add_one(struct ib_device *device)
1172 {
1173         int devnum;
1174         dev_t base;
1175         struct ib_uverbs_device *uverbs_dev;
1176         int ret;
1177
1178         if (!device->ops.alloc_ucontext)
1179                 return;
1180
1181         uverbs_dev = kzalloc(sizeof(*uverbs_dev), GFP_KERNEL);
1182         if (!uverbs_dev)
1183                 return;
1184
1185         ret = init_srcu_struct(&uverbs_dev->disassociate_srcu);
1186         if (ret) {
1187                 kfree(uverbs_dev);
1188                 return;
1189         }
1190
1191         device_initialize(&uverbs_dev->dev);
1192         uverbs_dev->dev.class = uverbs_class;
1193         uverbs_dev->dev.parent = device->dev.parent;
1194         uverbs_dev->dev.release = ib_uverbs_release_dev;
1195         uverbs_dev->groups[0] = &dev_attr_group;
1196         uverbs_dev->dev.groups = uverbs_dev->groups;
1197         atomic_set(&uverbs_dev->refcount, 1);
1198         init_completion(&uverbs_dev->comp);
1199         uverbs_dev->xrcd_tree = RB_ROOT;
1200         mutex_init(&uverbs_dev->xrcd_tree_mutex);
1201         mutex_init(&uverbs_dev->lists_mutex);
1202         INIT_LIST_HEAD(&uverbs_dev->uverbs_file_list);
1203         INIT_LIST_HEAD(&uverbs_dev->uverbs_events_file_list);
1204         rcu_assign_pointer(uverbs_dev->ib_dev, device);
1205         uverbs_dev->num_comp_vectors = device->num_comp_vectors;
1206
1207         devnum = ida_alloc_max(&uverbs_ida, IB_UVERBS_MAX_DEVICES - 1,
1208                                GFP_KERNEL);
1209         if (devnum < 0)
1210                 goto err;
1211         uverbs_dev->devnum = devnum;
1212         if (devnum >= IB_UVERBS_NUM_FIXED_MINOR)
1213                 base = dynamic_uverbs_dev + devnum - IB_UVERBS_NUM_FIXED_MINOR;
1214         else
1215                 base = IB_UVERBS_BASE_DEV + devnum;
1216
1217         if (ib_uverbs_create_uapi(device, uverbs_dev))
1218                 goto err_uapi;
1219
1220         uverbs_dev->dev.devt = base;
1221         dev_set_name(&uverbs_dev->dev, "uverbs%d", uverbs_dev->devnum);
1222
1223         cdev_init(&uverbs_dev->cdev,
1224                   device->ops.mmap ? &uverbs_mmap_fops : &uverbs_fops);
1225         uverbs_dev->cdev.owner = THIS_MODULE;
1226
1227         ret = cdev_device_add(&uverbs_dev->cdev, &uverbs_dev->dev);
1228         if (ret)
1229                 goto err_uapi;
1230
1231         ib_set_client_data(device, &uverbs_client, uverbs_dev);
1232         return;
1233
1234 err_uapi:
1235         ida_free(&uverbs_ida, devnum);
1236 err:
1237         if (atomic_dec_and_test(&uverbs_dev->refcount))
1238                 ib_uverbs_comp_dev(uverbs_dev);
1239         wait_for_completion(&uverbs_dev->comp);
1240         put_device(&uverbs_dev->dev);
1241         return;
1242 }
1243
1244 static void ib_uverbs_free_hw_resources(struct ib_uverbs_device *uverbs_dev,
1245                                         struct ib_device *ib_dev)
1246 {
1247         struct ib_uverbs_file *file;
1248         struct ib_uverbs_async_event_file *event_file;
1249         struct ib_event event;
1250
1251         /* Pending running commands to terminate */
1252         uverbs_disassociate_api_pre(uverbs_dev);
1253         event.event = IB_EVENT_DEVICE_FATAL;
1254         event.element.port_num = 0;
1255         event.device = ib_dev;
1256
1257         mutex_lock(&uverbs_dev->lists_mutex);
1258         while (!list_empty(&uverbs_dev->uverbs_file_list)) {
1259                 file = list_first_entry(&uverbs_dev->uverbs_file_list,
1260                                         struct ib_uverbs_file, list);
1261                 list_del_init(&file->list);
1262                 kref_get(&file->ref);
1263
1264                 /* We must release the mutex before going ahead and calling
1265                  * uverbs_cleanup_ufile, as it might end up indirectly calling
1266                  * uverbs_close, for example due to freeing the resources (e.g
1267                  * mmput).
1268                  */
1269                 mutex_unlock(&uverbs_dev->lists_mutex);
1270
1271                 ib_uverbs_event_handler(&file->event_handler, &event);
1272                 uverbs_destroy_ufile_hw(file, RDMA_REMOVE_DRIVER_REMOVE);
1273                 kref_put(&file->ref, ib_uverbs_release_file);
1274
1275                 mutex_lock(&uverbs_dev->lists_mutex);
1276         }
1277
1278         while (!list_empty(&uverbs_dev->uverbs_events_file_list)) {
1279                 event_file = list_first_entry(&uverbs_dev->
1280                                               uverbs_events_file_list,
1281                                               struct ib_uverbs_async_event_file,
1282                                               list);
1283                 spin_lock_irq(&event_file->ev_queue.lock);
1284                 event_file->ev_queue.is_closed = 1;
1285                 spin_unlock_irq(&event_file->ev_queue.lock);
1286
1287                 list_del(&event_file->list);
1288                 ib_unregister_event_handler(
1289                         &event_file->uverbs_file->event_handler);
1290                 event_file->uverbs_file->event_handler.device =
1291                         NULL;
1292
1293                 wake_up_interruptible(&event_file->ev_queue.poll_wait);
1294                 kill_fasync(&event_file->ev_queue.async_queue, SIGIO, POLL_IN);
1295         }
1296         mutex_unlock(&uverbs_dev->lists_mutex);
1297
1298         uverbs_disassociate_api(uverbs_dev->uapi);
1299 }
1300
1301 static void ib_uverbs_remove_one(struct ib_device *device, void *client_data)
1302 {
1303         struct ib_uverbs_device *uverbs_dev = client_data;
1304         int wait_clients = 1;
1305
1306         if (!uverbs_dev)
1307                 return;
1308
1309         cdev_device_del(&uverbs_dev->cdev, &uverbs_dev->dev);
1310         ida_free(&uverbs_ida, uverbs_dev->devnum);
1311
1312         if (device->ops.disassociate_ucontext) {
1313                 /* We disassociate HW resources and immediately return.
1314                  * Userspace will see a EIO errno for all future access.
1315                  * Upon returning, ib_device may be freed internally and is not
1316                  * valid any more.
1317                  * uverbs_device is still available until all clients close
1318                  * their files, then the uverbs device ref count will be zero
1319                  * and its resources will be freed.
1320                  * Note: At this point no more files can be opened since the
1321                  * cdev was deleted, however active clients can still issue
1322                  * commands and close their open files.
1323                  */
1324                 ib_uverbs_free_hw_resources(uverbs_dev, device);
1325                 wait_clients = 0;
1326         }
1327
1328         if (atomic_dec_and_test(&uverbs_dev->refcount))
1329                 ib_uverbs_comp_dev(uverbs_dev);
1330         if (wait_clients)
1331                 wait_for_completion(&uverbs_dev->comp);
1332
1333         put_device(&uverbs_dev->dev);
1334 }
1335
1336 static char *uverbs_devnode(struct device *dev, umode_t *mode)
1337 {
1338         if (mode)
1339                 *mode = 0666;
1340         return kasprintf(GFP_KERNEL, "infiniband/%s", dev_name(dev));
1341 }
1342
1343 static int __init ib_uverbs_init(void)
1344 {
1345         int ret;
1346
1347         ret = register_chrdev_region(IB_UVERBS_BASE_DEV,
1348                                      IB_UVERBS_NUM_FIXED_MINOR,
1349                                      "infiniband_verbs");
1350         if (ret) {
1351                 pr_err("user_verbs: couldn't register device number\n");
1352                 goto out;
1353         }
1354
1355         ret = alloc_chrdev_region(&dynamic_uverbs_dev, 0,
1356                                   IB_UVERBS_NUM_DYNAMIC_MINOR,
1357                                   "infiniband_verbs");
1358         if (ret) {
1359                 pr_err("couldn't register dynamic device number\n");
1360                 goto out_alloc;
1361         }
1362
1363         uverbs_class = class_create(THIS_MODULE, "infiniband_verbs");
1364         if (IS_ERR(uverbs_class)) {
1365                 ret = PTR_ERR(uverbs_class);
1366                 pr_err("user_verbs: couldn't create class infiniband_verbs\n");
1367                 goto out_chrdev;
1368         }
1369
1370         uverbs_class->devnode = uverbs_devnode;
1371
1372         ret = class_create_file(uverbs_class, &class_attr_abi_version.attr);
1373         if (ret) {
1374                 pr_err("user_verbs: couldn't create abi_version attribute\n");
1375                 goto out_class;
1376         }
1377
1378         ret = ib_register_client(&uverbs_client);
1379         if (ret) {
1380                 pr_err("user_verbs: couldn't register client\n");
1381                 goto out_class;
1382         }
1383
1384         return 0;
1385
1386 out_class:
1387         class_destroy(uverbs_class);
1388
1389 out_chrdev:
1390         unregister_chrdev_region(dynamic_uverbs_dev,
1391                                  IB_UVERBS_NUM_DYNAMIC_MINOR);
1392
1393 out_alloc:
1394         unregister_chrdev_region(IB_UVERBS_BASE_DEV,
1395                                  IB_UVERBS_NUM_FIXED_MINOR);
1396
1397 out:
1398         return ret;
1399 }
1400
1401 static void __exit ib_uverbs_cleanup(void)
1402 {
1403         ib_unregister_client(&uverbs_client);
1404         class_destroy(uverbs_class);
1405         unregister_chrdev_region(IB_UVERBS_BASE_DEV,
1406                                  IB_UVERBS_NUM_FIXED_MINOR);
1407         unregister_chrdev_region(dynamic_uverbs_dev,
1408                                  IB_UVERBS_NUM_DYNAMIC_MINOR);
1409         mmu_notifier_synchronize();
1410 }
1411
1412 module_init(ib_uverbs_init);
1413 module_exit(ib_uverbs_cleanup);