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