hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/drivers/infiniband/core/uverbs_main.c
....@@ -45,11 +45,13 @@
4545 #include <linux/cdev.h>
4646 #include <linux/anon_inodes.h>
4747 #include <linux/slab.h>
48
+#include <linux/sched/mm.h>
4849
4950 #include <linux/uaccess.h>
5051
5152 #include <rdma/ib.h>
5253 #include <rdma/uverbs_std_types.h>
54
+#include <rdma/rdma_netlink.h>
5355
5456 #include "uverbs.h"
5557 #include "core_priv.h"
....@@ -72,73 +74,15 @@
7274 static dev_t dynamic_uverbs_dev;
7375 static struct class *uverbs_class;
7476
75
-static DECLARE_BITMAP(dev_map, IB_UVERBS_MAX_DEVICES);
76
-
77
-static ssize_t (*uverbs_cmd_table[])(struct ib_uverbs_file *file,
78
- const char __user *buf, int in_len,
79
- int out_len) = {
80
- [IB_USER_VERBS_CMD_GET_CONTEXT] = ib_uverbs_get_context,
81
- [IB_USER_VERBS_CMD_QUERY_DEVICE] = ib_uverbs_query_device,
82
- [IB_USER_VERBS_CMD_QUERY_PORT] = ib_uverbs_query_port,
83
- [IB_USER_VERBS_CMD_ALLOC_PD] = ib_uverbs_alloc_pd,
84
- [IB_USER_VERBS_CMD_DEALLOC_PD] = ib_uverbs_dealloc_pd,
85
- [IB_USER_VERBS_CMD_REG_MR] = ib_uverbs_reg_mr,
86
- [IB_USER_VERBS_CMD_REREG_MR] = ib_uverbs_rereg_mr,
87
- [IB_USER_VERBS_CMD_DEREG_MR] = ib_uverbs_dereg_mr,
88
- [IB_USER_VERBS_CMD_ALLOC_MW] = ib_uverbs_alloc_mw,
89
- [IB_USER_VERBS_CMD_DEALLOC_MW] = ib_uverbs_dealloc_mw,
90
- [IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL] = ib_uverbs_create_comp_channel,
91
- [IB_USER_VERBS_CMD_CREATE_CQ] = ib_uverbs_create_cq,
92
- [IB_USER_VERBS_CMD_RESIZE_CQ] = ib_uverbs_resize_cq,
93
- [IB_USER_VERBS_CMD_POLL_CQ] = ib_uverbs_poll_cq,
94
- [IB_USER_VERBS_CMD_REQ_NOTIFY_CQ] = ib_uverbs_req_notify_cq,
95
- [IB_USER_VERBS_CMD_DESTROY_CQ] = ib_uverbs_destroy_cq,
96
- [IB_USER_VERBS_CMD_CREATE_QP] = ib_uverbs_create_qp,
97
- [IB_USER_VERBS_CMD_QUERY_QP] = ib_uverbs_query_qp,
98
- [IB_USER_VERBS_CMD_MODIFY_QP] = ib_uverbs_modify_qp,
99
- [IB_USER_VERBS_CMD_DESTROY_QP] = ib_uverbs_destroy_qp,
100
- [IB_USER_VERBS_CMD_POST_SEND] = ib_uverbs_post_send,
101
- [IB_USER_VERBS_CMD_POST_RECV] = ib_uverbs_post_recv,
102
- [IB_USER_VERBS_CMD_POST_SRQ_RECV] = ib_uverbs_post_srq_recv,
103
- [IB_USER_VERBS_CMD_CREATE_AH] = ib_uverbs_create_ah,
104
- [IB_USER_VERBS_CMD_DESTROY_AH] = ib_uverbs_destroy_ah,
105
- [IB_USER_VERBS_CMD_ATTACH_MCAST] = ib_uverbs_attach_mcast,
106
- [IB_USER_VERBS_CMD_DETACH_MCAST] = ib_uverbs_detach_mcast,
107
- [IB_USER_VERBS_CMD_CREATE_SRQ] = ib_uverbs_create_srq,
108
- [IB_USER_VERBS_CMD_MODIFY_SRQ] = ib_uverbs_modify_srq,
109
- [IB_USER_VERBS_CMD_QUERY_SRQ] = ib_uverbs_query_srq,
110
- [IB_USER_VERBS_CMD_DESTROY_SRQ] = ib_uverbs_destroy_srq,
111
- [IB_USER_VERBS_CMD_OPEN_XRCD] = ib_uverbs_open_xrcd,
112
- [IB_USER_VERBS_CMD_CLOSE_XRCD] = ib_uverbs_close_xrcd,
113
- [IB_USER_VERBS_CMD_CREATE_XSRQ] = ib_uverbs_create_xsrq,
114
- [IB_USER_VERBS_CMD_OPEN_QP] = ib_uverbs_open_qp,
115
-};
116
-
117
-static int (*uverbs_ex_cmd_table[])(struct ib_uverbs_file *file,
118
- struct ib_udata *ucore,
119
- struct ib_udata *uhw) = {
120
- [IB_USER_VERBS_EX_CMD_CREATE_FLOW] = ib_uverbs_ex_create_flow,
121
- [IB_USER_VERBS_EX_CMD_DESTROY_FLOW] = ib_uverbs_ex_destroy_flow,
122
- [IB_USER_VERBS_EX_CMD_QUERY_DEVICE] = ib_uverbs_ex_query_device,
123
- [IB_USER_VERBS_EX_CMD_CREATE_CQ] = ib_uverbs_ex_create_cq,
124
- [IB_USER_VERBS_EX_CMD_CREATE_QP] = ib_uverbs_ex_create_qp,
125
- [IB_USER_VERBS_EX_CMD_CREATE_WQ] = ib_uverbs_ex_create_wq,
126
- [IB_USER_VERBS_EX_CMD_MODIFY_WQ] = ib_uverbs_ex_modify_wq,
127
- [IB_USER_VERBS_EX_CMD_DESTROY_WQ] = ib_uverbs_ex_destroy_wq,
128
- [IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL] = ib_uverbs_ex_create_rwq_ind_table,
129
- [IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL] = ib_uverbs_ex_destroy_rwq_ind_table,
130
- [IB_USER_VERBS_EX_CMD_MODIFY_QP] = ib_uverbs_ex_modify_qp,
131
- [IB_USER_VERBS_EX_CMD_MODIFY_CQ] = ib_uverbs_ex_modify_cq,
132
-};
133
-
134
-static void ib_uverbs_add_one(struct ib_device *device);
77
+static DEFINE_IDA(uverbs_ida);
78
+static int ib_uverbs_add_one(struct ib_device *device);
13579 static void ib_uverbs_remove_one(struct ib_device *device, void *client_data);
13680
13781 /*
13882 * Must be called with the ufile->device->disassociate_srcu held, and the lock
13983 * must be held until use of the ucontext is finished.
14084 */
141
-struct ib_ucontext *ib_uverbs_get_ucontext(struct ib_uverbs_file *ufile)
85
+struct ib_ucontext *ib_uverbs_get_ucontext_file(struct ib_uverbs_file *ufile)
14286 {
14387 /*
14488 * We do not hold the hw_destroy_rwsem lock for this flow, instead
....@@ -156,44 +100,36 @@
156100
157101 return ucontext;
158102 }
159
-EXPORT_SYMBOL(ib_uverbs_get_ucontext);
103
+EXPORT_SYMBOL(ib_uverbs_get_ucontext_file);
160104
161105 int uverbs_dealloc_mw(struct ib_mw *mw)
162106 {
163107 struct ib_pd *pd = mw->pd;
164108 int ret;
165109
166
- ret = mw->device->dealloc_mw(mw);
167
- if (!ret)
168
- atomic_dec(&pd->usecnt);
110
+ ret = mw->device->ops.dealloc_mw(mw);
111
+ if (ret)
112
+ return ret;
113
+
114
+ atomic_dec(&pd->usecnt);
115
+ kfree(mw);
169116 return ret;
170117 }
171118
172
-static void ib_uverbs_release_dev(struct kobject *kobj)
119
+static void ib_uverbs_release_dev(struct device *device)
173120 {
174121 struct ib_uverbs_device *dev =
175
- container_of(kobj, struct ib_uverbs_device, kobj);
122
+ container_of(device, struct ib_uverbs_device, dev);
176123
177124 uverbs_destroy_api(dev->uapi);
178125 cleanup_srcu_struct(&dev->disassociate_srcu);
126
+ mutex_destroy(&dev->lists_mutex);
127
+ mutex_destroy(&dev->xrcd_tree_mutex);
179128 kfree(dev);
180129 }
181130
182
-static struct kobj_type ib_uverbs_dev_ktype = {
183
- .release = ib_uverbs_release_dev,
184
-};
185
-
186
-static void ib_uverbs_release_async_event_file(struct kref *ref)
187
-{
188
- struct ib_uverbs_async_event_file *file =
189
- container_of(ref, struct ib_uverbs_async_event_file, ref);
190
-
191
- kfree(file);
192
-}
193
-
194
-void ib_uverbs_release_ucq(struct ib_uverbs_file *file,
195
- struct ib_uverbs_completion_event_file *ev_file,
196
- struct ib_ucq_object *uobj)
131
+void ib_uverbs_release_ucq(struct ib_uverbs_completion_event_file *ev_file,
132
+ struct ib_ucq_object *uobj)
197133 {
198134 struct ib_uverbs_event *evt, *tmp;
199135
....@@ -208,25 +144,24 @@
208144 uverbs_uobject_put(&ev_file->uobj);
209145 }
210146
211
- spin_lock_irq(&file->async_file->ev_queue.lock);
212
- list_for_each_entry_safe(evt, tmp, &uobj->async_list, obj_list) {
213
- list_del(&evt->list);
214
- kfree(evt);
215
- }
216
- spin_unlock_irq(&file->async_file->ev_queue.lock);
147
+ ib_uverbs_release_uevent(&uobj->uevent);
217148 }
218149
219
-void ib_uverbs_release_uevent(struct ib_uverbs_file *file,
220
- struct ib_uevent_object *uobj)
150
+void ib_uverbs_release_uevent(struct ib_uevent_object *uobj)
221151 {
152
+ struct ib_uverbs_async_event_file *async_file = uobj->event_file;
222153 struct ib_uverbs_event *evt, *tmp;
223154
224
- spin_lock_irq(&file->async_file->ev_queue.lock);
155
+ if (!async_file)
156
+ return;
157
+
158
+ spin_lock_irq(&async_file->ev_queue.lock);
225159 list_for_each_entry_safe(evt, tmp, &uobj->event_list, obj_list) {
226160 list_del(&evt->list);
227161 kfree(evt);
228162 }
229
- spin_unlock_irq(&file->async_file->ev_queue.lock);
163
+ spin_unlock_irq(&async_file->ev_queue.lock);
164
+ uverbs_uobject_put(&async_file->uobj);
230165 }
231166
232167 void ib_uverbs_detach_umcast(struct ib_qp *qp,
....@@ -258,17 +193,21 @@
258193 srcu_key = srcu_read_lock(&file->device->disassociate_srcu);
259194 ib_dev = srcu_dereference(file->device->ib_dev,
260195 &file->device->disassociate_srcu);
261
- if (ib_dev && !ib_dev->disassociate_ucontext)
262
- module_put(ib_dev->owner);
196
+ if (ib_dev && !ib_dev->ops.disassociate_ucontext)
197
+ module_put(ib_dev->ops.owner);
263198 srcu_read_unlock(&file->device->disassociate_srcu, srcu_key);
264199
265200 if (atomic_dec_and_test(&file->device->refcount))
266201 ib_uverbs_comp_dev(file->device);
267202
268
- if (file->async_file)
269
- kref_put(&file->async_file->ref,
270
- ib_uverbs_release_async_event_file);
271
- kobject_put(&file->device->kobj);
203
+ if (file->default_async_file)
204
+ uverbs_uobject_put(&file->default_async_file->uobj);
205
+ put_device(&file->device->dev);
206
+
207
+ if (file->disassociate_page)
208
+ __free_pages(file->disassociate_page, 0);
209
+ mutex_destroy(&file->umap_lock);
210
+ mutex_destroy(&file->ucontext_lock);
272211 kfree(file);
273212 }
274213
....@@ -283,8 +222,12 @@
283222 spin_lock_irq(&ev_queue->lock);
284223
285224 while (list_empty(&ev_queue->event_list)) {
286
- spin_unlock_irq(&ev_queue->lock);
225
+ if (ev_queue->is_closed) {
226
+ spin_unlock_irq(&ev_queue->lock);
227
+ return -EIO;
228
+ }
287229
230
+ spin_unlock_irq(&ev_queue->lock);
288231 if (filp->f_flags & O_NONBLOCK)
289232 return -EAGAIN;
290233
....@@ -294,12 +237,6 @@
294237 return -ERESTARTSYS;
295238
296239 spin_lock_irq(&ev_queue->lock);
297
-
298
- /* If device was disassociated and no event exists set an error */
299
- if (list_empty(&ev_queue->event_list) && ev_queue->is_closed) {
300
- spin_unlock_irq(&ev_queue->lock);
301
- return -EIO;
302
- }
303240 }
304241
305242 event = list_entry(ev_queue->event_list.next, struct ib_uverbs_event, list);
....@@ -399,70 +336,20 @@
399336 return fasync_helper(fd, filp, on, &comp_ev_file->ev_queue.async_queue);
400337 }
401338
402
-static int ib_uverbs_async_event_close(struct inode *inode, struct file *filp)
403
-{
404
- struct ib_uverbs_async_event_file *file = filp->private_data;
405
- struct ib_uverbs_file *uverbs_file = file->uverbs_file;
406
- struct ib_uverbs_event *entry, *tmp;
407
- int closed_already = 0;
408
-
409
- mutex_lock(&uverbs_file->device->lists_mutex);
410
- spin_lock_irq(&file->ev_queue.lock);
411
- closed_already = file->ev_queue.is_closed;
412
- file->ev_queue.is_closed = 1;
413
- list_for_each_entry_safe(entry, tmp, &file->ev_queue.event_list, list) {
414
- if (entry->counter)
415
- list_del(&entry->obj_list);
416
- kfree(entry);
417
- }
418
- spin_unlock_irq(&file->ev_queue.lock);
419
- if (!closed_already) {
420
- list_del(&file->list);
421
- ib_unregister_event_handler(&uverbs_file->event_handler);
422
- }
423
- mutex_unlock(&uverbs_file->device->lists_mutex);
424
-
425
- kref_put(&uverbs_file->ref, ib_uverbs_release_file);
426
- kref_put(&file->ref, ib_uverbs_release_async_event_file);
427
-
428
- return 0;
429
-}
430
-
431
-static int ib_uverbs_comp_event_close(struct inode *inode, struct file *filp)
432
-{
433
- struct ib_uobject *uobj = filp->private_data;
434
- struct ib_uverbs_completion_event_file *file = container_of(
435
- uobj, struct ib_uverbs_completion_event_file, uobj);
436
- struct ib_uverbs_event *entry, *tmp;
437
-
438
- spin_lock_irq(&file->ev_queue.lock);
439
- list_for_each_entry_safe(entry, tmp, &file->ev_queue.event_list, list) {
440
- if (entry->counter)
441
- list_del(&entry->obj_list);
442
- kfree(entry);
443
- }
444
- file->ev_queue.is_closed = 1;
445
- spin_unlock_irq(&file->ev_queue.lock);
446
-
447
- uverbs_close_fd(filp);
448
-
449
- return 0;
450
-}
451
-
452339 const struct file_operations uverbs_event_fops = {
453340 .owner = THIS_MODULE,
454341 .read = ib_uverbs_comp_event_read,
455342 .poll = ib_uverbs_comp_event_poll,
456
- .release = ib_uverbs_comp_event_close,
343
+ .release = uverbs_uobject_fd_release,
457344 .fasync = ib_uverbs_comp_event_fasync,
458345 .llseek = no_llseek,
459346 };
460347
461
-static const struct file_operations uverbs_async_event_fops = {
348
+const struct file_operations uverbs_async_event_fops = {
462349 .owner = THIS_MODULE,
463350 .read = ib_uverbs_async_event_read,
464351 .poll = ib_uverbs_async_event_poll,
465
- .release = ib_uverbs_async_event_close,
352
+ .release = uverbs_async_event_release,
466353 .fasync = ib_uverbs_async_event_fasync,
467354 .llseek = no_llseek,
468355 };
....@@ -489,9 +376,9 @@
489376 return;
490377 }
491378
492
- uobj = container_of(cq->uobject, struct ib_ucq_object, uobject);
379
+ uobj = cq->uobject;
493380
494
- entry->desc.comp.cq_handle = cq->uobject->user_handle;
381
+ entry->desc.comp.cq_handle = cq->uobject->uevent.uobject.user_handle;
495382 entry->counter = &uobj->comp_events_reported;
496383
497384 list_add_tail(&entry->list, &ev_queue->event_list);
....@@ -502,102 +389,81 @@
502389 kill_fasync(&ev_queue->async_queue, SIGIO, POLL_IN);
503390 }
504391
505
-static void ib_uverbs_async_handler(struct ib_uverbs_file *file,
506
- __u64 element, __u64 event,
507
- struct list_head *obj_list,
508
- u32 *counter)
392
+void ib_uverbs_async_handler(struct ib_uverbs_async_event_file *async_file,
393
+ __u64 element, __u64 event,
394
+ struct list_head *obj_list, u32 *counter)
509395 {
510396 struct ib_uverbs_event *entry;
511397 unsigned long flags;
512398
513
- spin_lock_irqsave(&file->async_file->ev_queue.lock, flags);
514
- if (file->async_file->ev_queue.is_closed) {
515
- spin_unlock_irqrestore(&file->async_file->ev_queue.lock, flags);
399
+ if (!async_file)
400
+ return;
401
+
402
+ spin_lock_irqsave(&async_file->ev_queue.lock, flags);
403
+ if (async_file->ev_queue.is_closed) {
404
+ spin_unlock_irqrestore(&async_file->ev_queue.lock, flags);
516405 return;
517406 }
518407
519408 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
520409 if (!entry) {
521
- spin_unlock_irqrestore(&file->async_file->ev_queue.lock, flags);
410
+ spin_unlock_irqrestore(&async_file->ev_queue.lock, flags);
522411 return;
523412 }
524413
525
- entry->desc.async.element = element;
414
+ entry->desc.async.element = element;
526415 entry->desc.async.event_type = event;
527
- entry->desc.async.reserved = 0;
528
- entry->counter = counter;
416
+ entry->desc.async.reserved = 0;
417
+ entry->counter = counter;
529418
530
- list_add_tail(&entry->list, &file->async_file->ev_queue.event_list);
419
+ list_add_tail(&entry->list, &async_file->ev_queue.event_list);
531420 if (obj_list)
532421 list_add_tail(&entry->obj_list, obj_list);
533
- spin_unlock_irqrestore(&file->async_file->ev_queue.lock, flags);
422
+ spin_unlock_irqrestore(&async_file->ev_queue.lock, flags);
534423
535
- wake_up_interruptible(&file->async_file->ev_queue.poll_wait);
536
- kill_fasync(&file->async_file->ev_queue.async_queue, SIGIO, POLL_IN);
424
+ wake_up_interruptible(&async_file->ev_queue.poll_wait);
425
+ kill_fasync(&async_file->ev_queue.async_queue, SIGIO, POLL_IN);
426
+}
427
+
428
+static void uverbs_uobj_event(struct ib_uevent_object *eobj,
429
+ struct ib_event *event)
430
+{
431
+ ib_uverbs_async_handler(eobj->event_file,
432
+ eobj->uobject.user_handle, event->event,
433
+ &eobj->event_list, &eobj->events_reported);
537434 }
538435
539436 void ib_uverbs_cq_event_handler(struct ib_event *event, void *context_ptr)
540437 {
541
- struct ib_ucq_object *uobj = container_of(event->element.cq->uobject,
542
- struct ib_ucq_object, uobject);
543
-
544
- ib_uverbs_async_handler(uobj->uobject.ufile, uobj->uobject.user_handle,
545
- event->event, &uobj->async_list,
546
- &uobj->async_events_reported);
438
+ uverbs_uobj_event(&event->element.cq->uobject->uevent, event);
547439 }
548440
549441 void ib_uverbs_qp_event_handler(struct ib_event *event, void *context_ptr)
550442 {
551
- struct ib_uevent_object *uobj;
552
-
553443 /* for XRC target qp's, check that qp is live */
554444 if (!event->element.qp->uobject)
555445 return;
556446
557
- uobj = container_of(event->element.qp->uobject,
558
- struct ib_uevent_object, uobject);
559
-
560
- ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle,
561
- event->event, &uobj->event_list,
562
- &uobj->events_reported);
447
+ uverbs_uobj_event(&event->element.qp->uobject->uevent, event);
563448 }
564449
565450 void ib_uverbs_wq_event_handler(struct ib_event *event, void *context_ptr)
566451 {
567
- struct ib_uevent_object *uobj = container_of(event->element.wq->uobject,
568
- struct ib_uevent_object, uobject);
569
-
570
- ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle,
571
- event->event, &uobj->event_list,
572
- &uobj->events_reported);
452
+ uverbs_uobj_event(&event->element.wq->uobject->uevent, event);
573453 }
574454
575455 void ib_uverbs_srq_event_handler(struct ib_event *event, void *context_ptr)
576456 {
577
- struct ib_uevent_object *uobj;
578
-
579
- uobj = container_of(event->element.srq->uobject,
580
- struct ib_uevent_object, uobject);
581
-
582
- ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle,
583
- event->event, &uobj->event_list,
584
- &uobj->events_reported);
457
+ uverbs_uobj_event(&event->element.srq->uobject->uevent, event);
585458 }
586459
587
-void ib_uverbs_event_handler(struct ib_event_handler *handler,
588
- struct ib_event *event)
460
+static void ib_uverbs_event_handler(struct ib_event_handler *handler,
461
+ struct ib_event *event)
589462 {
590
- struct ib_uverbs_file *file =
591
- container_of(handler, struct ib_uverbs_file, event_handler);
592
-
593
- ib_uverbs_async_handler(file, event->element.port_num, event->event,
594
- NULL, NULL);
595
-}
596
-
597
-void ib_uverbs_free_async_event_file(struct ib_uverbs_file *file)
598
-{
599
- kref_put(&file->async_file->ref, ib_uverbs_release_async_event_file);
600
- file->async_file = NULL;
463
+ ib_uverbs_async_handler(
464
+ container_of(handler, struct ib_uverbs_async_event_file,
465
+ event_handler),
466
+ event->element.port_num, event->event, NULL, NULL);
601467 }
602468
603469 void ib_uverbs_init_event_queue(struct ib_uverbs_event_queue *ev_queue)
....@@ -609,91 +475,40 @@
609475 ev_queue->async_queue = NULL;
610476 }
611477
612
-struct file *ib_uverbs_alloc_async_event_file(struct ib_uverbs_file *uverbs_file,
613
- struct ib_device *ib_dev)
478
+void ib_uverbs_init_async_event_file(
479
+ struct ib_uverbs_async_event_file *async_file)
614480 {
615
- struct ib_uverbs_async_event_file *ev_file;
616
- struct file *filp;
481
+ struct ib_uverbs_file *uverbs_file = async_file->uobj.ufile;
482
+ struct ib_device *ib_dev = async_file->uobj.context->device;
617483
618
- ev_file = kzalloc(sizeof(*ev_file), GFP_KERNEL);
619
- if (!ev_file)
620
- return ERR_PTR(-ENOMEM);
484
+ ib_uverbs_init_event_queue(&async_file->ev_queue);
621485
622
- ib_uverbs_init_event_queue(&ev_file->ev_queue);
623
- ev_file->uverbs_file = uverbs_file;
624
- kref_get(&ev_file->uverbs_file->ref);
625
- kref_init(&ev_file->ref);
626
- filp = anon_inode_getfile("[infinibandevent]", &uverbs_async_event_fops,
627
- ev_file, O_RDONLY);
628
- if (IS_ERR(filp))
629
- goto err_put_refs;
486
+ /* The first async_event_file becomes the default one for the file. */
487
+ mutex_lock(&uverbs_file->ucontext_lock);
488
+ if (!uverbs_file->default_async_file) {
489
+ /* Pairs with the put in ib_uverbs_release_file */
490
+ uverbs_uobject_get(&async_file->uobj);
491
+ smp_store_release(&uverbs_file->default_async_file, async_file);
492
+ }
493
+ mutex_unlock(&uverbs_file->ucontext_lock);
630494
631
- mutex_lock(&uverbs_file->device->lists_mutex);
632
- list_add_tail(&ev_file->list,
633
- &uverbs_file->device->uverbs_events_file_list);
634
- mutex_unlock(&uverbs_file->device->lists_mutex);
635
-
636
- WARN_ON(uverbs_file->async_file);
637
- uverbs_file->async_file = ev_file;
638
- kref_get(&uverbs_file->async_file->ref);
639
- INIT_IB_EVENT_HANDLER(&uverbs_file->event_handler,
640
- ib_dev,
495
+ INIT_IB_EVENT_HANDLER(&async_file->event_handler, ib_dev,
641496 ib_uverbs_event_handler);
642
- ib_register_event_handler(&uverbs_file->event_handler);
643
- /* At that point async file stuff was fully set */
644
-
645
- return filp;
646
-
647
-err_put_refs:
648
- kref_put(&ev_file->uverbs_file->ref, ib_uverbs_release_file);
649
- kref_put(&ev_file->ref, ib_uverbs_release_async_event_file);
650
- return filp;
651
-}
652
-
653
-static bool verify_command_mask(struct ib_uverbs_file *ufile, u32 command,
654
- bool extended)
655
-{
656
- if (!extended)
657
- return ufile->uverbs_cmd_mask & BIT_ULL(command);
658
-
659
- return ufile->uverbs_ex_cmd_mask & BIT_ULL(command);
660
-}
661
-
662
-static bool verify_command_idx(u32 command, bool extended)
663
-{
664
- if (extended)
665
- return command < ARRAY_SIZE(uverbs_ex_cmd_table) &&
666
- uverbs_ex_cmd_table[command];
667
-
668
- return command < ARRAY_SIZE(uverbs_cmd_table) &&
669
- uverbs_cmd_table[command];
670
-}
671
-
672
-static ssize_t process_hdr(struct ib_uverbs_cmd_hdr *hdr,
673
- u32 *command, bool *extended)
674
-{
675
- if (hdr->command & ~(u32)(IB_USER_VERBS_CMD_FLAG_EXTENDED |
676
- IB_USER_VERBS_CMD_COMMAND_MASK))
677
- return -EINVAL;
678
-
679
- *command = hdr->command & IB_USER_VERBS_CMD_COMMAND_MASK;
680
- *extended = hdr->command & IB_USER_VERBS_CMD_FLAG_EXTENDED;
681
-
682
- if (!verify_command_idx(*command, *extended))
683
- return -EOPNOTSUPP;
684
-
685
- return 0;
497
+ ib_register_event_handler(&async_file->event_handler);
686498 }
687499
688500 static ssize_t verify_hdr(struct ib_uverbs_cmd_hdr *hdr,
689
- struct ib_uverbs_ex_cmd_hdr *ex_hdr,
690
- size_t count, bool extended)
501
+ struct ib_uverbs_ex_cmd_hdr *ex_hdr, size_t count,
502
+ const struct uverbs_api_write_method *method_elm)
691503 {
692
- if (extended) {
504
+ if (method_elm->is_ex) {
693505 count -= sizeof(*hdr) + sizeof(*ex_hdr);
694506
695507 if ((hdr->in_words + ex_hdr->provider_in_words) * 8 != count)
696508 return -EINVAL;
509
+
510
+ if (hdr->in_words * 8 < method_elm->req_size)
511
+ return -ENOSPC;
697512
698513 if (ex_hdr->cmd_hdr_reserved)
699514 return -EINVAL;
....@@ -702,8 +517,10 @@
702517 if (!hdr->out_words && !ex_hdr->provider_out_words)
703518 return -EINVAL;
704519
705
- if (!access_ok(VERIFY_WRITE,
706
- u64_to_user_ptr(ex_hdr->response),
520
+ if (hdr->out_words * 8 < method_elm->resp_size)
521
+ return -ENOSPC;
522
+
523
+ if (!access_ok(u64_to_user_ptr(ex_hdr->response),
707524 (hdr->out_words + ex_hdr->provider_out_words) * 8))
708525 return -EFAULT;
709526 } else {
....@@ -718,6 +535,24 @@
718535 if (hdr->in_words * 4 != count)
719536 return -EINVAL;
720537
538
+ if (count < method_elm->req_size + sizeof(*hdr)) {
539
+ /*
540
+ * rdma-core v18 and v19 have a bug where they send DESTROY_CQ
541
+ * with a 16 byte write instead of 24. Old kernels didn't
542
+ * check the size so they allowed this. Now that the size is
543
+ * checked provide a compatibility work around to not break
544
+ * those userspaces.
545
+ */
546
+ if (hdr->command == IB_USER_VERBS_CMD_DESTROY_CQ &&
547
+ count == 16) {
548
+ hdr->in_words = 6;
549
+ return 0;
550
+ }
551
+ return -ENOSPC;
552
+ }
553
+ if (hdr->out_words * 4 < method_elm->resp_size)
554
+ return -ENOSPC;
555
+
721556 return 0;
722557 }
723558
....@@ -725,11 +560,12 @@
725560 size_t count, loff_t *pos)
726561 {
727562 struct ib_uverbs_file *file = filp->private_data;
563
+ const struct uverbs_api_write_method *method_elm;
564
+ struct uverbs_api *uapi = file->device->uapi;
728565 struct ib_uverbs_ex_cmd_hdr ex_hdr;
729566 struct ib_uverbs_cmd_hdr hdr;
730
- bool extended;
567
+ struct uverbs_attr_bundle bundle;
731568 int srcu_key;
732
- u32 command;
733569 ssize_t ret;
734570
735571 if (!ib_safe_file_access(filp)) {
....@@ -744,58 +580,101 @@
744580 if (copy_from_user(&hdr, buf, sizeof(hdr)))
745581 return -EFAULT;
746582
747
- ret = process_hdr(&hdr, &command, &extended);
748
- if (ret)
749
- return ret;
583
+ method_elm = uapi_get_method(uapi, hdr.command);
584
+ if (IS_ERR(method_elm))
585
+ return PTR_ERR(method_elm);
750586
751
- if (extended) {
587
+ if (method_elm->is_ex) {
752588 if (count < (sizeof(hdr) + sizeof(ex_hdr)))
753589 return -EINVAL;
754590 if (copy_from_user(&ex_hdr, buf + sizeof(hdr), sizeof(ex_hdr)))
755591 return -EFAULT;
756592 }
757593
758
- ret = verify_hdr(&hdr, &ex_hdr, count, extended);
594
+ ret = verify_hdr(&hdr, &ex_hdr, count, method_elm);
759595 if (ret)
760596 return ret;
761597
762598 srcu_key = srcu_read_lock(&file->device->disassociate_srcu);
763599
764
- if (!verify_command_mask(file, command, extended)) {
765
- ret = -EOPNOTSUPP;
766
- goto out;
767
- }
768
-
769600 buf += sizeof(hdr);
770601
771
- if (!extended) {
772
- ret = uverbs_cmd_table[command](file, buf,
773
- hdr.in_words * 4,
774
- hdr.out_words * 4);
775
- } else {
776
- struct ib_udata ucore;
777
- struct ib_udata uhw;
602
+ memset(bundle.attr_present, 0, sizeof(bundle.attr_present));
603
+ bundle.ufile = file;
604
+ bundle.context = NULL; /* only valid if bundle has uobject */
605
+ bundle.uobject = NULL;
606
+ if (!method_elm->is_ex) {
607
+ size_t in_len = hdr.in_words * 4 - sizeof(hdr);
608
+ size_t out_len = hdr.out_words * 4;
609
+ u64 response = 0;
778610
611
+ if (method_elm->has_udata) {
612
+ bundle.driver_udata.inlen =
613
+ in_len - method_elm->req_size;
614
+ in_len = method_elm->req_size;
615
+ if (bundle.driver_udata.inlen)
616
+ bundle.driver_udata.inbuf = buf + in_len;
617
+ else
618
+ bundle.driver_udata.inbuf = NULL;
619
+ } else {
620
+ memset(&bundle.driver_udata, 0,
621
+ sizeof(bundle.driver_udata));
622
+ }
623
+
624
+ if (method_elm->has_resp) {
625
+ /*
626
+ * The macros check that if has_resp is set
627
+ * then the command request structure starts
628
+ * with a '__aligned u64 response' member.
629
+ */
630
+ ret = get_user(response, (const u64 __user *)buf);
631
+ if (ret)
632
+ goto out_unlock;
633
+
634
+ if (method_elm->has_udata) {
635
+ bundle.driver_udata.outlen =
636
+ out_len - method_elm->resp_size;
637
+ out_len = method_elm->resp_size;
638
+ if (bundle.driver_udata.outlen)
639
+ bundle.driver_udata.outbuf =
640
+ u64_to_user_ptr(response +
641
+ out_len);
642
+ else
643
+ bundle.driver_udata.outbuf = NULL;
644
+ }
645
+ } else {
646
+ bundle.driver_udata.outlen = 0;
647
+ bundle.driver_udata.outbuf = NULL;
648
+ }
649
+
650
+ ib_uverbs_init_udata_buf_or_null(
651
+ &bundle.ucore, buf, u64_to_user_ptr(response),
652
+ in_len, out_len);
653
+ } else {
779654 buf += sizeof(ex_hdr);
780655
781
- ib_uverbs_init_udata_buf_or_null(&ucore, buf,
656
+ ib_uverbs_init_udata_buf_or_null(&bundle.ucore, buf,
782657 u64_to_user_ptr(ex_hdr.response),
783658 hdr.in_words * 8, hdr.out_words * 8);
784659
785
- ib_uverbs_init_udata_buf_or_null(&uhw,
786
- buf + ucore.inlen,
787
- u64_to_user_ptr(ex_hdr.response) + ucore.outlen,
788
- ex_hdr.provider_in_words * 8,
789
- ex_hdr.provider_out_words * 8);
660
+ ib_uverbs_init_udata_buf_or_null(
661
+ &bundle.driver_udata, buf + bundle.ucore.inlen,
662
+ u64_to_user_ptr(ex_hdr.response) + bundle.ucore.outlen,
663
+ ex_hdr.provider_in_words * 8,
664
+ ex_hdr.provider_out_words * 8);
790665
791
- ret = uverbs_ex_cmd_table[command](file, &ucore, &uhw);
792
- ret = (ret) ? : count;
793666 }
794667
795
-out:
668
+ ret = method_elm->handler(&bundle);
669
+ if (bundle.uobject)
670
+ uverbs_finalize_object(bundle.uobject, UVERBS_ACCESS_NEW, true,
671
+ !ret, &bundle);
672
+out_unlock:
796673 srcu_read_unlock(&file->device->disassociate_srcu, srcu_key);
797
- return ret;
674
+ return (ret) ? : count;
798675 }
676
+
677
+static const struct vm_operations_struct rdma_umap_ops;
799678
800679 static int ib_uverbs_mmap(struct file *filp, struct vm_area_struct *vma)
801680 {
....@@ -805,16 +684,189 @@
805684 int srcu_key;
806685
807686 srcu_key = srcu_read_lock(&file->device->disassociate_srcu);
808
- ucontext = ib_uverbs_get_ucontext(file);
687
+ ucontext = ib_uverbs_get_ucontext_file(file);
809688 if (IS_ERR(ucontext)) {
810689 ret = PTR_ERR(ucontext);
811690 goto out;
812691 }
813
-
814
- ret = ucontext->device->mmap(ucontext, vma);
692
+ vma->vm_ops = &rdma_umap_ops;
693
+ ret = ucontext->device->ops.mmap(ucontext, vma);
815694 out:
816695 srcu_read_unlock(&file->device->disassociate_srcu, srcu_key);
817696 return ret;
697
+}
698
+
699
+/*
700
+ * The VMA has been dup'd, initialize the vm_private_data with a new tracking
701
+ * struct
702
+ */
703
+static void rdma_umap_open(struct vm_area_struct *vma)
704
+{
705
+ struct ib_uverbs_file *ufile = vma->vm_file->private_data;
706
+ struct rdma_umap_priv *opriv = vma->vm_private_data;
707
+ struct rdma_umap_priv *priv;
708
+
709
+ if (!opriv)
710
+ return;
711
+
712
+ /* We are racing with disassociation */
713
+ if (!down_read_trylock(&ufile->hw_destroy_rwsem))
714
+ goto out_zap;
715
+ /*
716
+ * Disassociation already completed, the VMA should already be zapped.
717
+ */
718
+ if (!ufile->ucontext)
719
+ goto out_unlock;
720
+
721
+ priv = kzalloc(sizeof(*priv), GFP_KERNEL);
722
+ if (!priv)
723
+ goto out_unlock;
724
+ rdma_umap_priv_init(priv, vma, opriv->entry);
725
+
726
+ up_read(&ufile->hw_destroy_rwsem);
727
+ return;
728
+
729
+out_unlock:
730
+ up_read(&ufile->hw_destroy_rwsem);
731
+out_zap:
732
+ /*
733
+ * We can't allow the VMA to be created with the actual IO pages, that
734
+ * would break our API contract, and it can't be stopped at this
735
+ * point, so zap it.
736
+ */
737
+ vma->vm_private_data = NULL;
738
+ zap_vma_ptes(vma, vma->vm_start, vma->vm_end - vma->vm_start);
739
+}
740
+
741
+static void rdma_umap_close(struct vm_area_struct *vma)
742
+{
743
+ struct ib_uverbs_file *ufile = vma->vm_file->private_data;
744
+ struct rdma_umap_priv *priv = vma->vm_private_data;
745
+
746
+ if (!priv)
747
+ return;
748
+
749
+ /*
750
+ * The vma holds a reference on the struct file that created it, which
751
+ * in turn means that the ib_uverbs_file is guaranteed to exist at
752
+ * this point.
753
+ */
754
+ mutex_lock(&ufile->umap_lock);
755
+ if (priv->entry)
756
+ rdma_user_mmap_entry_put(priv->entry);
757
+
758
+ list_del(&priv->list);
759
+ mutex_unlock(&ufile->umap_lock);
760
+ kfree(priv);
761
+}
762
+
763
+/*
764
+ * Once the zap_vma_ptes has been called touches to the VMA will come here and
765
+ * we return a dummy writable zero page for all the pfns.
766
+ */
767
+static vm_fault_t rdma_umap_fault(struct vm_fault *vmf)
768
+{
769
+ struct ib_uverbs_file *ufile = vmf->vma->vm_file->private_data;
770
+ struct rdma_umap_priv *priv = vmf->vma->vm_private_data;
771
+ vm_fault_t ret = 0;
772
+
773
+ if (!priv)
774
+ return VM_FAULT_SIGBUS;
775
+
776
+ /* Read only pages can just use the system zero page. */
777
+ if (!(vmf->vma->vm_flags & (VM_WRITE | VM_MAYWRITE))) {
778
+ vmf->page = ZERO_PAGE(vmf->address);
779
+ get_page(vmf->page);
780
+ return 0;
781
+ }
782
+
783
+ mutex_lock(&ufile->umap_lock);
784
+ if (!ufile->disassociate_page)
785
+ ufile->disassociate_page =
786
+ alloc_pages(vmf->gfp_mask | __GFP_ZERO, 0);
787
+
788
+ if (ufile->disassociate_page) {
789
+ /*
790
+ * This VMA is forced to always be shared so this doesn't have
791
+ * to worry about COW.
792
+ */
793
+ vmf->page = ufile->disassociate_page;
794
+ get_page(vmf->page);
795
+ } else {
796
+ ret = VM_FAULT_SIGBUS;
797
+ }
798
+ mutex_unlock(&ufile->umap_lock);
799
+
800
+ return ret;
801
+}
802
+
803
+static const struct vm_operations_struct rdma_umap_ops = {
804
+ .open = rdma_umap_open,
805
+ .close = rdma_umap_close,
806
+ .fault = rdma_umap_fault,
807
+};
808
+
809
+void uverbs_user_mmap_disassociate(struct ib_uverbs_file *ufile)
810
+{
811
+ struct rdma_umap_priv *priv, *next_priv;
812
+
813
+ lockdep_assert_held(&ufile->hw_destroy_rwsem);
814
+
815
+ while (1) {
816
+ struct mm_struct *mm = NULL;
817
+
818
+ /* Get an arbitrary mm pointer that hasn't been cleaned yet */
819
+ mutex_lock(&ufile->umap_lock);
820
+ while (!list_empty(&ufile->umaps)) {
821
+ int ret;
822
+
823
+ priv = list_first_entry(&ufile->umaps,
824
+ struct rdma_umap_priv, list);
825
+ mm = priv->vma->vm_mm;
826
+ ret = mmget_not_zero(mm);
827
+ if (!ret) {
828
+ list_del_init(&priv->list);
829
+ if (priv->entry) {
830
+ rdma_user_mmap_entry_put(priv->entry);
831
+ priv->entry = NULL;
832
+ }
833
+ mm = NULL;
834
+ continue;
835
+ }
836
+ break;
837
+ }
838
+ mutex_unlock(&ufile->umap_lock);
839
+ if (!mm)
840
+ return;
841
+
842
+ /*
843
+ * The umap_lock is nested under mmap_lock since it used within
844
+ * the vma_ops callbacks, so we have to clean the list one mm
845
+ * at a time to get the lock ordering right. Typically there
846
+ * will only be one mm, so no big deal.
847
+ */
848
+ mmap_read_lock(mm);
849
+ mutex_lock(&ufile->umap_lock);
850
+ list_for_each_entry_safe (priv, next_priv, &ufile->umaps,
851
+ list) {
852
+ struct vm_area_struct *vma = priv->vma;
853
+
854
+ if (vma->vm_mm != mm)
855
+ continue;
856
+ list_del_init(&priv->list);
857
+
858
+ zap_vma_ptes(vma, vma->vm_start,
859
+ vma->vm_end - vma->vm_start);
860
+
861
+ if (priv->entry) {
862
+ rdma_user_mmap_entry_put(priv->entry);
863
+ priv->entry = NULL;
864
+ }
865
+ }
866
+ mutex_unlock(&ufile->umap_lock);
867
+ mmap_read_unlock(mm);
868
+ mmput(mm);
869
+ }
818870 }
819871
820872 /*
....@@ -840,6 +892,7 @@
840892 if (!atomic_inc_not_zero(&dev->refcount))
841893 return -ENXIO;
842894
895
+ get_device(&dev->dev);
843896 srcu_key = srcu_read_lock(&dev->disassociate_srcu);
844897 mutex_lock(&dev->lists_mutex);
845898 ib_dev = srcu_dereference(dev->ib_dev,
....@@ -849,13 +902,18 @@
849902 goto err;
850903 }
851904
905
+ if (!rdma_dev_access_netns(ib_dev, current->nsproxy->net_ns)) {
906
+ ret = -EPERM;
907
+ goto err;
908
+ }
909
+
852910 /* In case IB device supports disassociate ucontext, there is no hard
853911 * dependency between uverbs device and its low level device.
854912 */
855
- module_dependent = !(ib_dev->disassociate_ucontext);
913
+ module_dependent = !(ib_dev->ops.disassociate_ucontext);
856914
857915 if (module_dependent) {
858
- if (!try_module_get(ib_dev->owner)) {
916
+ if (!try_module_get(ib_dev->ops.owner)) {
859917 ret = -ENODEV;
860918 goto err;
861919 }
....@@ -877,22 +935,20 @@
877935 spin_lock_init(&file->uobjects_lock);
878936 INIT_LIST_HEAD(&file->uobjects);
879937 init_rwsem(&file->hw_destroy_rwsem);
938
+ mutex_init(&file->umap_lock);
939
+ INIT_LIST_HEAD(&file->umaps);
880940
881941 filp->private_data = file;
882
- kobject_get(&dev->kobj);
883942 list_add_tail(&file->list, &dev->uverbs_file_list);
884943 mutex_unlock(&dev->lists_mutex);
885944 srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
886945
887
- file->uverbs_cmd_mask = ib_dev->uverbs_cmd_mask;
888
- file->uverbs_ex_cmd_mask = ib_dev->uverbs_ex_cmd_mask;
889
-
890946 setup_ufile_idr_uobject(file);
891947
892
- return nonseekable_open(inode, filp);
948
+ return stream_open(inode, filp);
893949
894950 err_module:
895
- module_put(ib_dev->owner);
951
+ module_put(ib_dev->ops.owner);
896952
897953 err:
898954 mutex_unlock(&dev->lists_mutex);
....@@ -900,6 +956,7 @@
900956 if (atomic_dec_and_test(&dev->refcount))
901957 ib_uverbs_comp_dev(dev);
902958
959
+ put_device(&dev->dev);
903960 return ret;
904961 }
905962
....@@ -910,10 +967,7 @@
910967 uverbs_destroy_ufile_hw(file, RDMA_REMOVE_CLOSE);
911968
912969 mutex_lock(&file->device->lists_mutex);
913
- if (!file->is_closed) {
914
- list_del(&file->list);
915
- file->is_closed = 1;
916
- }
970
+ list_del_init(&file->list);
917971 mutex_unlock(&file->device->lists_mutex);
918972
919973 kref_put(&file->ref, ib_uverbs_release_file);
....@@ -928,7 +982,7 @@
928982 .release = ib_uverbs_close,
929983 .llseek = no_llseek,
930984 .unlocked_ioctl = ib_uverbs_ioctl,
931
- .compat_ioctl = ib_uverbs_ioctl,
985
+ .compat_ioctl = compat_ptr_ioctl,
932986 };
933987
934988 static const struct file_operations uverbs_mmap_fops = {
....@@ -939,55 +993,92 @@
939993 .release = ib_uverbs_close,
940994 .llseek = no_llseek,
941995 .unlocked_ioctl = ib_uverbs_ioctl,
942
- .compat_ioctl = ib_uverbs_ioctl,
996
+ .compat_ioctl = compat_ptr_ioctl,
943997 };
998
+
999
+static int ib_uverbs_get_nl_info(struct ib_device *ibdev, void *client_data,
1000
+ struct ib_client_nl_info *res)
1001
+{
1002
+ struct ib_uverbs_device *uverbs_dev = client_data;
1003
+ int ret;
1004
+
1005
+ if (res->port != -1)
1006
+ return -EINVAL;
1007
+
1008
+ res->abi = ibdev->ops.uverbs_abi_ver;
1009
+ res->cdev = &uverbs_dev->dev;
1010
+
1011
+ /*
1012
+ * To support DRIVER_ID binding in userspace some of the driver need
1013
+ * upgrading to expose their PCI dependent revision information
1014
+ * through get_context instead of relying on modalias matching. When
1015
+ * the drivers are fixed they can drop this flag.
1016
+ */
1017
+ if (!ibdev->ops.uverbs_no_driver_id_binding) {
1018
+ ret = nla_put_u32(res->nl_msg, RDMA_NLDEV_ATTR_UVERBS_DRIVER_ID,
1019
+ ibdev->ops.driver_id);
1020
+ if (ret)
1021
+ return ret;
1022
+ }
1023
+ return 0;
1024
+}
9441025
9451026 static struct ib_client uverbs_client = {
9461027 .name = "uverbs",
1028
+ .no_kverbs_req = true,
9471029 .add = ib_uverbs_add_one,
948
- .remove = ib_uverbs_remove_one
1030
+ .remove = ib_uverbs_remove_one,
1031
+ .get_nl_info = ib_uverbs_get_nl_info,
9491032 };
1033
+MODULE_ALIAS_RDMA_CLIENT("uverbs");
9501034
951
-static ssize_t show_ibdev(struct device *device, struct device_attribute *attr,
1035
+static ssize_t ibdev_show(struct device *device, struct device_attribute *attr,
9521036 char *buf)
9531037 {
1038
+ struct ib_uverbs_device *dev =
1039
+ container_of(device, struct ib_uverbs_device, dev);
9541040 int ret = -ENODEV;
9551041 int srcu_key;
956
- struct ib_uverbs_device *dev = dev_get_drvdata(device);
9571042 struct ib_device *ib_dev;
958
-
959
- if (!dev)
960
- return -ENODEV;
9611043
9621044 srcu_key = srcu_read_lock(&dev->disassociate_srcu);
9631045 ib_dev = srcu_dereference(dev->ib_dev, &dev->disassociate_srcu);
9641046 if (ib_dev)
965
- ret = sprintf(buf, "%s\n", ib_dev->name);
1047
+ ret = sprintf(buf, "%s\n", dev_name(&ib_dev->dev));
9661048 srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
9671049
9681050 return ret;
9691051 }
970
-static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
1052
+static DEVICE_ATTR_RO(ibdev);
9711053
972
-static ssize_t show_dev_abi_version(struct device *device,
973
- struct device_attribute *attr, char *buf)
1054
+static ssize_t abi_version_show(struct device *device,
1055
+ struct device_attribute *attr, char *buf)
9741056 {
975
- struct ib_uverbs_device *dev = dev_get_drvdata(device);
1057
+ struct ib_uverbs_device *dev =
1058
+ container_of(device, struct ib_uverbs_device, dev);
9761059 int ret = -ENODEV;
9771060 int srcu_key;
9781061 struct ib_device *ib_dev;
9791062
980
- if (!dev)
981
- return -ENODEV;
9821063 srcu_key = srcu_read_lock(&dev->disassociate_srcu);
9831064 ib_dev = srcu_dereference(dev->ib_dev, &dev->disassociate_srcu);
9841065 if (ib_dev)
985
- ret = sprintf(buf, "%d\n", ib_dev->uverbs_abi_ver);
1066
+ ret = sprintf(buf, "%u\n", ib_dev->ops.uverbs_abi_ver);
9861067 srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
9871068
9881069 return ret;
9891070 }
990
-static DEVICE_ATTR(abi_version, S_IRUGO, show_dev_abi_version, NULL);
1071
+static DEVICE_ATTR_RO(abi_version);
1072
+
1073
+static struct attribute *ib_dev_attrs[] = {
1074
+ &dev_attr_abi_version.attr,
1075
+ &dev_attr_ibdev.attr,
1076
+ NULL,
1077
+};
1078
+
1079
+static const struct attribute_group dev_attr_group = {
1080
+ .attrs = ib_dev_attrs,
1081
+};
9911082
9921083 static CLASS_ATTR_STRING(abi_version, S_IRUGO,
9931084 __stringify(IB_USER_VERBS_ABI_VERSION));
....@@ -997,7 +1088,7 @@
9971088 {
9981089 struct uverbs_api *uapi;
9991090
1000
- uapi = uverbs_alloc_api(device->driver_specs, device->driver_id);
1091
+ uapi = uverbs_alloc_api(device);
10011092 if (IS_ERR(uapi))
10021093 return PTR_ERR(uapi);
10031094
....@@ -1005,107 +1096,94 @@
10051096 return 0;
10061097 }
10071098
1008
-static void ib_uverbs_add_one(struct ib_device *device)
1099
+static int ib_uverbs_add_one(struct ib_device *device)
10091100 {
10101101 int devnum;
10111102 dev_t base;
10121103 struct ib_uverbs_device *uverbs_dev;
10131104 int ret;
10141105
1015
- if (!device->alloc_ucontext)
1016
- return;
1106
+ if (!device->ops.alloc_ucontext)
1107
+ return -EOPNOTSUPP;
10171108
10181109 uverbs_dev = kzalloc(sizeof(*uverbs_dev), GFP_KERNEL);
10191110 if (!uverbs_dev)
1020
- return;
1111
+ return -ENOMEM;
10211112
10221113 ret = init_srcu_struct(&uverbs_dev->disassociate_srcu);
10231114 if (ret) {
10241115 kfree(uverbs_dev);
1025
- return;
1116
+ return -ENOMEM;
10261117 }
10271118
1119
+ device_initialize(&uverbs_dev->dev);
1120
+ uverbs_dev->dev.class = uverbs_class;
1121
+ uverbs_dev->dev.parent = device->dev.parent;
1122
+ uverbs_dev->dev.release = ib_uverbs_release_dev;
1123
+ uverbs_dev->groups[0] = &dev_attr_group;
1124
+ uverbs_dev->dev.groups = uverbs_dev->groups;
10281125 atomic_set(&uverbs_dev->refcount, 1);
10291126 init_completion(&uverbs_dev->comp);
10301127 uverbs_dev->xrcd_tree = RB_ROOT;
10311128 mutex_init(&uverbs_dev->xrcd_tree_mutex);
1032
- kobject_init(&uverbs_dev->kobj, &ib_uverbs_dev_ktype);
10331129 mutex_init(&uverbs_dev->lists_mutex);
10341130 INIT_LIST_HEAD(&uverbs_dev->uverbs_file_list);
1035
- INIT_LIST_HEAD(&uverbs_dev->uverbs_events_file_list);
1131
+ rcu_assign_pointer(uverbs_dev->ib_dev, device);
1132
+ uverbs_dev->num_comp_vectors = device->num_comp_vectors;
10361133
1037
- devnum = find_first_zero_bit(dev_map, IB_UVERBS_MAX_DEVICES);
1038
- if (devnum >= IB_UVERBS_MAX_DEVICES)
1134
+ devnum = ida_alloc_max(&uverbs_ida, IB_UVERBS_MAX_DEVICES - 1,
1135
+ GFP_KERNEL);
1136
+ if (devnum < 0) {
1137
+ ret = -ENOMEM;
10391138 goto err;
1139
+ }
10401140 uverbs_dev->devnum = devnum;
1041
- set_bit(devnum, dev_map);
10421141 if (devnum >= IB_UVERBS_NUM_FIXED_MINOR)
10431142 base = dynamic_uverbs_dev + devnum - IB_UVERBS_NUM_FIXED_MINOR;
10441143 else
10451144 base = IB_UVERBS_BASE_DEV + devnum;
10461145
1047
- rcu_assign_pointer(uverbs_dev->ib_dev, device);
1048
- uverbs_dev->num_comp_vectors = device->num_comp_vectors;
1049
-
1050
- if (ib_uverbs_create_uapi(device, uverbs_dev))
1146
+ ret = ib_uverbs_create_uapi(device, uverbs_dev);
1147
+ if (ret)
10511148 goto err_uapi;
10521149
1053
- cdev_init(&uverbs_dev->cdev, NULL);
1150
+ uverbs_dev->dev.devt = base;
1151
+ dev_set_name(&uverbs_dev->dev, "uverbs%d", uverbs_dev->devnum);
1152
+
1153
+ cdev_init(&uverbs_dev->cdev,
1154
+ device->ops.mmap ? &uverbs_mmap_fops : &uverbs_fops);
10541155 uverbs_dev->cdev.owner = THIS_MODULE;
1055
- uverbs_dev->cdev.ops = device->mmap ? &uverbs_mmap_fops : &uverbs_fops;
1056
- cdev_set_parent(&uverbs_dev->cdev, &uverbs_dev->kobj);
1057
- kobject_set_name(&uverbs_dev->cdev.kobj, "uverbs%d", uverbs_dev->devnum);
1058
- if (cdev_add(&uverbs_dev->cdev, base, 1))
1059
- goto err_cdev;
10601156
1061
- uverbs_dev->dev = device_create(uverbs_class, device->dev.parent,
1062
- uverbs_dev->cdev.dev, uverbs_dev,
1063
- "uverbs%d", uverbs_dev->devnum);
1064
- if (IS_ERR(uverbs_dev->dev))
1065
- goto err_cdev;
1066
-
1067
- if (device_create_file(uverbs_dev->dev, &dev_attr_ibdev))
1068
- goto err_class;
1069
- if (device_create_file(uverbs_dev->dev, &dev_attr_abi_version))
1070
- goto err_class;
1157
+ ret = cdev_device_add(&uverbs_dev->cdev, &uverbs_dev->dev);
1158
+ if (ret)
1159
+ goto err_uapi;
10711160
10721161 ib_set_client_data(device, &uverbs_client, uverbs_dev);
1162
+ return 0;
10731163
1074
- return;
1075
-
1076
-err_class:
1077
- device_destroy(uverbs_class, uverbs_dev->cdev.dev);
1078
-err_cdev:
1079
- cdev_del(&uverbs_dev->cdev);
10801164 err_uapi:
1081
- clear_bit(devnum, dev_map);
1165
+ ida_free(&uverbs_ida, devnum);
10821166 err:
10831167 if (atomic_dec_and_test(&uverbs_dev->refcount))
10841168 ib_uverbs_comp_dev(uverbs_dev);
10851169 wait_for_completion(&uverbs_dev->comp);
1086
- kobject_put(&uverbs_dev->kobj);
1087
- return;
1170
+ put_device(&uverbs_dev->dev);
1171
+ return ret;
10881172 }
10891173
10901174 static void ib_uverbs_free_hw_resources(struct ib_uverbs_device *uverbs_dev,
10911175 struct ib_device *ib_dev)
10921176 {
10931177 struct ib_uverbs_file *file;
1094
- struct ib_uverbs_async_event_file *event_file;
1095
- struct ib_event event;
10961178
10971179 /* Pending running commands to terminate */
10981180 uverbs_disassociate_api_pre(uverbs_dev);
1099
- event.event = IB_EVENT_DEVICE_FATAL;
1100
- event.element.port_num = 0;
1101
- event.device = ib_dev;
11021181
11031182 mutex_lock(&uverbs_dev->lists_mutex);
11041183 while (!list_empty(&uverbs_dev->uverbs_file_list)) {
11051184 file = list_first_entry(&uverbs_dev->uverbs_file_list,
11061185 struct ib_uverbs_file, list);
1107
- file->is_closed = 1;
1108
- list_del(&file->list);
1186
+ list_del_init(&file->list);
11091187 kref_get(&file->ref);
11101188
11111189 /* We must release the mutex before going ahead and calling
....@@ -1115,30 +1193,10 @@
11151193 */
11161194 mutex_unlock(&uverbs_dev->lists_mutex);
11171195
1118
- ib_uverbs_event_handler(&file->event_handler, &event);
11191196 uverbs_destroy_ufile_hw(file, RDMA_REMOVE_DRIVER_REMOVE);
11201197 kref_put(&file->ref, ib_uverbs_release_file);
11211198
11221199 mutex_lock(&uverbs_dev->lists_mutex);
1123
- }
1124
-
1125
- while (!list_empty(&uverbs_dev->uverbs_events_file_list)) {
1126
- event_file = list_first_entry(&uverbs_dev->
1127
- uverbs_events_file_list,
1128
- struct ib_uverbs_async_event_file,
1129
- list);
1130
- spin_lock_irq(&event_file->ev_queue.lock);
1131
- event_file->ev_queue.is_closed = 1;
1132
- spin_unlock_irq(&event_file->ev_queue.lock);
1133
-
1134
- list_del(&event_file->list);
1135
- ib_unregister_event_handler(
1136
- &event_file->uverbs_file->event_handler);
1137
- event_file->uverbs_file->event_handler.device =
1138
- NULL;
1139
-
1140
- wake_up_interruptible(&event_file->ev_queue.poll_wait);
1141
- kill_fasync(&event_file->ev_queue.async_queue, SIGIO, POLL_IN);
11421200 }
11431201 mutex_unlock(&uverbs_dev->lists_mutex);
11441202
....@@ -1150,15 +1208,10 @@
11501208 struct ib_uverbs_device *uverbs_dev = client_data;
11511209 int wait_clients = 1;
11521210
1153
- if (!uverbs_dev)
1154
- return;
1211
+ cdev_device_del(&uverbs_dev->cdev, &uverbs_dev->dev);
1212
+ ida_free(&uverbs_ida, uverbs_dev->devnum);
11551213
1156
- dev_set_drvdata(uverbs_dev->dev, NULL);
1157
- device_destroy(uverbs_class, uverbs_dev->cdev.dev);
1158
- cdev_del(&uverbs_dev->cdev);
1159
- clear_bit(uverbs_dev->devnum, dev_map);
1160
-
1161
- if (device->disassociate_ucontext) {
1214
+ if (device->ops.disassociate_ucontext) {
11621215 /* We disassociate HW resources and immediately return.
11631216 * Userspace will see a EIO errno for all future access.
11641217 * Upon returning, ib_device may be freed internally and is not
....@@ -1179,7 +1232,7 @@
11791232 if (wait_clients)
11801233 wait_for_completion(&uverbs_dev->comp);
11811234
1182
- kobject_put(&uverbs_dev->kobj);
1235
+ put_device(&uverbs_dev->dev);
11831236 }
11841237
11851238 static char *uverbs_devnode(struct device *dev, umode_t *mode)
....@@ -1255,6 +1308,7 @@
12551308 IB_UVERBS_NUM_FIXED_MINOR);
12561309 unregister_chrdev_region(dynamic_uverbs_dev,
12571310 IB_UVERBS_NUM_DYNAMIC_MINOR);
1311
+ mmu_notifier_synchronize();
12581312 }
12591313
12601314 module_init(ib_uverbs_init);