hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
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
....@@ -399,70 +338,20 @@
399338 return fasync_helper(fd, filp, on, &comp_ev_file->ev_queue.async_queue);
400339 }
401340
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
-
452341 const struct file_operations uverbs_event_fops = {
453342 .owner = THIS_MODULE,
454343 .read = ib_uverbs_comp_event_read,
455344 .poll = ib_uverbs_comp_event_poll,
456
- .release = ib_uverbs_comp_event_close,
345
+ .release = uverbs_uobject_fd_release,
457346 .fasync = ib_uverbs_comp_event_fasync,
458347 .llseek = no_llseek,
459348 };
460349
461
-static const struct file_operations uverbs_async_event_fops = {
350
+const struct file_operations uverbs_async_event_fops = {
462351 .owner = THIS_MODULE,
463352 .read = ib_uverbs_async_event_read,
464353 .poll = ib_uverbs_async_event_poll,
465
- .release = ib_uverbs_async_event_close,
354
+ .release = uverbs_async_event_release,
466355 .fasync = ib_uverbs_async_event_fasync,
467356 .llseek = no_llseek,
468357 };
....@@ -489,9 +378,9 @@
489378 return;
490379 }
491380
492
- uobj = container_of(cq->uobject, struct ib_ucq_object, uobject);
381
+ uobj = cq->uobject;
493382
494
- entry->desc.comp.cq_handle = cq->uobject->user_handle;
383
+ entry->desc.comp.cq_handle = cq->uobject->uevent.uobject.user_handle;
495384 entry->counter = &uobj->comp_events_reported;
496385
497386 list_add_tail(&entry->list, &ev_queue->event_list);
....@@ -502,102 +391,81 @@
502391 kill_fasync(&ev_queue->async_queue, SIGIO, POLL_IN);
503392 }
504393
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)
394
+void ib_uverbs_async_handler(struct ib_uverbs_async_event_file *async_file,
395
+ __u64 element, __u64 event,
396
+ struct list_head *obj_list, u32 *counter)
509397 {
510398 struct ib_uverbs_event *entry;
511399 unsigned long flags;
512400
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);
401
+ if (!async_file)
402
+ return;
403
+
404
+ spin_lock_irqsave(&async_file->ev_queue.lock, flags);
405
+ if (async_file->ev_queue.is_closed) {
406
+ spin_unlock_irqrestore(&async_file->ev_queue.lock, flags);
516407 return;
517408 }
518409
519410 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
520411 if (!entry) {
521
- spin_unlock_irqrestore(&file->async_file->ev_queue.lock, flags);
412
+ spin_unlock_irqrestore(&async_file->ev_queue.lock, flags);
522413 return;
523414 }
524415
525
- entry->desc.async.element = element;
416
+ entry->desc.async.element = element;
526417 entry->desc.async.event_type = event;
527
- entry->desc.async.reserved = 0;
528
- entry->counter = counter;
418
+ entry->desc.async.reserved = 0;
419
+ entry->counter = counter;
529420
530
- list_add_tail(&entry->list, &file->async_file->ev_queue.event_list);
421
+ list_add_tail(&entry->list, &async_file->ev_queue.event_list);
531422 if (obj_list)
532423 list_add_tail(&entry->obj_list, obj_list);
533
- spin_unlock_irqrestore(&file->async_file->ev_queue.lock, flags);
424
+ spin_unlock_irqrestore(&async_file->ev_queue.lock, flags);
534425
535
- wake_up_interruptible(&file->async_file->ev_queue.poll_wait);
536
- kill_fasync(&file->async_file->ev_queue.async_queue, SIGIO, POLL_IN);
426
+ wake_up_interruptible(&async_file->ev_queue.poll_wait);
427
+ kill_fasync(&async_file->ev_queue.async_queue, SIGIO, POLL_IN);
428
+}
429
+
430
+static void uverbs_uobj_event(struct ib_uevent_object *eobj,
431
+ struct ib_event *event)
432
+{
433
+ ib_uverbs_async_handler(eobj->event_file,
434
+ eobj->uobject.user_handle, event->event,
435
+ &eobj->event_list, &eobj->events_reported);
537436 }
538437
539438 void ib_uverbs_cq_event_handler(struct ib_event *event, void *context_ptr)
540439 {
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);
440
+ uverbs_uobj_event(&event->element.cq->uobject->uevent, event);
547441 }
548442
549443 void ib_uverbs_qp_event_handler(struct ib_event *event, void *context_ptr)
550444 {
551
- struct ib_uevent_object *uobj;
552
-
553445 /* for XRC target qp's, check that qp is live */
554446 if (!event->element.qp->uobject)
555447 return;
556448
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);
449
+ uverbs_uobj_event(&event->element.qp->uobject->uevent, event);
563450 }
564451
565452 void ib_uverbs_wq_event_handler(struct ib_event *event, void *context_ptr)
566453 {
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);
454
+ uverbs_uobj_event(&event->element.wq->uobject->uevent, event);
573455 }
574456
575457 void ib_uverbs_srq_event_handler(struct ib_event *event, void *context_ptr)
576458 {
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);
459
+ uverbs_uobj_event(&event->element.srq->uobject->uevent, event);
585460 }
586461
587
-void ib_uverbs_event_handler(struct ib_event_handler *handler,
588
- struct ib_event *event)
462
+static void ib_uverbs_event_handler(struct ib_event_handler *handler,
463
+ struct ib_event *event)
589464 {
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;
465
+ ib_uverbs_async_handler(
466
+ container_of(handler, struct ib_uverbs_async_event_file,
467
+ event_handler),
468
+ event->element.port_num, event->event, NULL, NULL);
601469 }
602470
603471 void ib_uverbs_init_event_queue(struct ib_uverbs_event_queue *ev_queue)
....@@ -609,91 +477,40 @@
609477 ev_queue->async_queue = NULL;
610478 }
611479
612
-struct file *ib_uverbs_alloc_async_event_file(struct ib_uverbs_file *uverbs_file,
613
- struct ib_device *ib_dev)
480
+void ib_uverbs_init_async_event_file(
481
+ struct ib_uverbs_async_event_file *async_file)
614482 {
615
- struct ib_uverbs_async_event_file *ev_file;
616
- struct file *filp;
483
+ struct ib_uverbs_file *uverbs_file = async_file->uobj.ufile;
484
+ struct ib_device *ib_dev = async_file->uobj.context->device;
617485
618
- ev_file = kzalloc(sizeof(*ev_file), GFP_KERNEL);
619
- if (!ev_file)
620
- return ERR_PTR(-ENOMEM);
486
+ ib_uverbs_init_event_queue(&async_file->ev_queue);
621487
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;
488
+ /* The first async_event_file becomes the default one for the file. */
489
+ mutex_lock(&uverbs_file->ucontext_lock);
490
+ if (!uverbs_file->default_async_file) {
491
+ /* Pairs with the put in ib_uverbs_release_file */
492
+ uverbs_uobject_get(&async_file->uobj);
493
+ smp_store_release(&uverbs_file->default_async_file, async_file);
494
+ }
495
+ mutex_unlock(&uverbs_file->ucontext_lock);
630496
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,
497
+ INIT_IB_EVENT_HANDLER(&async_file->event_handler, ib_dev,
641498 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;
499
+ ib_register_event_handler(&async_file->event_handler);
686500 }
687501
688502 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)
503
+ struct ib_uverbs_ex_cmd_hdr *ex_hdr, size_t count,
504
+ const struct uverbs_api_write_method *method_elm)
691505 {
692
- if (extended) {
506
+ if (method_elm->is_ex) {
693507 count -= sizeof(*hdr) + sizeof(*ex_hdr);
694508
695509 if ((hdr->in_words + ex_hdr->provider_in_words) * 8 != count)
696510 return -EINVAL;
511
+
512
+ if (hdr->in_words * 8 < method_elm->req_size)
513
+ return -ENOSPC;
697514
698515 if (ex_hdr->cmd_hdr_reserved)
699516 return -EINVAL;
....@@ -702,8 +519,10 @@
702519 if (!hdr->out_words && !ex_hdr->provider_out_words)
703520 return -EINVAL;
704521
705
- if (!access_ok(VERIFY_WRITE,
706
- u64_to_user_ptr(ex_hdr->response),
522
+ if (hdr->out_words * 8 < method_elm->resp_size)
523
+ return -ENOSPC;
524
+
525
+ if (!access_ok(u64_to_user_ptr(ex_hdr->response),
707526 (hdr->out_words + ex_hdr->provider_out_words) * 8))
708527 return -EFAULT;
709528 } else {
....@@ -718,6 +537,24 @@
718537 if (hdr->in_words * 4 != count)
719538 return -EINVAL;
720539
540
+ if (count < method_elm->req_size + sizeof(hdr)) {
541
+ /*
542
+ * rdma-core v18 and v19 have a bug where they send DESTROY_CQ
543
+ * with a 16 byte write instead of 24. Old kernels didn't
544
+ * check the size so they allowed this. Now that the size is
545
+ * checked provide a compatibility work around to not break
546
+ * those userspaces.
547
+ */
548
+ if (hdr->command == IB_USER_VERBS_CMD_DESTROY_CQ &&
549
+ count == 16) {
550
+ hdr->in_words = 6;
551
+ return 0;
552
+ }
553
+ return -ENOSPC;
554
+ }
555
+ if (hdr->out_words * 4 < method_elm->resp_size)
556
+ return -ENOSPC;
557
+
721558 return 0;
722559 }
723560
....@@ -725,11 +562,12 @@
725562 size_t count, loff_t *pos)
726563 {
727564 struct ib_uverbs_file *file = filp->private_data;
565
+ const struct uverbs_api_write_method *method_elm;
566
+ struct uverbs_api *uapi = file->device->uapi;
728567 struct ib_uverbs_ex_cmd_hdr ex_hdr;
729568 struct ib_uverbs_cmd_hdr hdr;
730
- bool extended;
569
+ struct uverbs_attr_bundle bundle;
731570 int srcu_key;
732
- u32 command;
733571 ssize_t ret;
734572
735573 if (!ib_safe_file_access(filp)) {
....@@ -744,58 +582,101 @@
744582 if (copy_from_user(&hdr, buf, sizeof(hdr)))
745583 return -EFAULT;
746584
747
- ret = process_hdr(&hdr, &command, &extended);
748
- if (ret)
749
- return ret;
585
+ method_elm = uapi_get_method(uapi, hdr.command);
586
+ if (IS_ERR(method_elm))
587
+ return PTR_ERR(method_elm);
750588
751
- if (extended) {
589
+ if (method_elm->is_ex) {
752590 if (count < (sizeof(hdr) + sizeof(ex_hdr)))
753591 return -EINVAL;
754592 if (copy_from_user(&ex_hdr, buf + sizeof(hdr), sizeof(ex_hdr)))
755593 return -EFAULT;
756594 }
757595
758
- ret = verify_hdr(&hdr, &ex_hdr, count, extended);
596
+ ret = verify_hdr(&hdr, &ex_hdr, count, method_elm);
759597 if (ret)
760598 return ret;
761599
762600 srcu_key = srcu_read_lock(&file->device->disassociate_srcu);
763601
764
- if (!verify_command_mask(file, command, extended)) {
765
- ret = -EOPNOTSUPP;
766
- goto out;
767
- }
768
-
769602 buf += sizeof(hdr);
770603
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;
604
+ memset(bundle.attr_present, 0, sizeof(bundle.attr_present));
605
+ bundle.ufile = file;
606
+ bundle.context = NULL; /* only valid if bundle has uobject */
607
+ bundle.uobject = NULL;
608
+ if (!method_elm->is_ex) {
609
+ size_t in_len = hdr.in_words * 4 - sizeof(hdr);
610
+ size_t out_len = hdr.out_words * 4;
611
+ u64 response = 0;
778612
613
+ if (method_elm->has_udata) {
614
+ bundle.driver_udata.inlen =
615
+ in_len - method_elm->req_size;
616
+ in_len = method_elm->req_size;
617
+ if (bundle.driver_udata.inlen)
618
+ bundle.driver_udata.inbuf = buf + in_len;
619
+ else
620
+ bundle.driver_udata.inbuf = NULL;
621
+ } else {
622
+ memset(&bundle.driver_udata, 0,
623
+ sizeof(bundle.driver_udata));
624
+ }
625
+
626
+ if (method_elm->has_resp) {
627
+ /*
628
+ * The macros check that if has_resp is set
629
+ * then the command request structure starts
630
+ * with a '__aligned u64 response' member.
631
+ */
632
+ ret = get_user(response, (const u64 __user *)buf);
633
+ if (ret)
634
+ goto out_unlock;
635
+
636
+ if (method_elm->has_udata) {
637
+ bundle.driver_udata.outlen =
638
+ out_len - method_elm->resp_size;
639
+ out_len = method_elm->resp_size;
640
+ if (bundle.driver_udata.outlen)
641
+ bundle.driver_udata.outbuf =
642
+ u64_to_user_ptr(response +
643
+ out_len);
644
+ else
645
+ bundle.driver_udata.outbuf = NULL;
646
+ }
647
+ } else {
648
+ bundle.driver_udata.outlen = 0;
649
+ bundle.driver_udata.outbuf = NULL;
650
+ }
651
+
652
+ ib_uverbs_init_udata_buf_or_null(
653
+ &bundle.ucore, buf, u64_to_user_ptr(response),
654
+ in_len, out_len);
655
+ } else {
779656 buf += sizeof(ex_hdr);
780657
781
- ib_uverbs_init_udata_buf_or_null(&ucore, buf,
658
+ ib_uverbs_init_udata_buf_or_null(&bundle.ucore, buf,
782659 u64_to_user_ptr(ex_hdr.response),
783660 hdr.in_words * 8, hdr.out_words * 8);
784661
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);
662
+ ib_uverbs_init_udata_buf_or_null(
663
+ &bundle.driver_udata, buf + bundle.ucore.inlen,
664
+ u64_to_user_ptr(ex_hdr.response) + bundle.ucore.outlen,
665
+ ex_hdr.provider_in_words * 8,
666
+ ex_hdr.provider_out_words * 8);
790667
791
- ret = uverbs_ex_cmd_table[command](file, &ucore, &uhw);
792
- ret = (ret) ? : count;
793668 }
794669
795
-out:
670
+ ret = method_elm->handler(&bundle);
671
+ if (bundle.uobject)
672
+ uverbs_finalize_object(bundle.uobject, UVERBS_ACCESS_NEW, true,
673
+ !ret, &bundle);
674
+out_unlock:
796675 srcu_read_unlock(&file->device->disassociate_srcu, srcu_key);
797
- return ret;
676
+ return (ret) ? : count;
798677 }
678
+
679
+static const struct vm_operations_struct rdma_umap_ops;
799680
800681 static int ib_uverbs_mmap(struct file *filp, struct vm_area_struct *vma)
801682 {
....@@ -805,16 +686,189 @@
805686 int srcu_key;
806687
807688 srcu_key = srcu_read_lock(&file->device->disassociate_srcu);
808
- ucontext = ib_uverbs_get_ucontext(file);
689
+ ucontext = ib_uverbs_get_ucontext_file(file);
809690 if (IS_ERR(ucontext)) {
810691 ret = PTR_ERR(ucontext);
811692 goto out;
812693 }
813
-
814
- ret = ucontext->device->mmap(ucontext, vma);
694
+ vma->vm_ops = &rdma_umap_ops;
695
+ ret = ucontext->device->ops.mmap(ucontext, vma);
815696 out:
816697 srcu_read_unlock(&file->device->disassociate_srcu, srcu_key);
817698 return ret;
699
+}
700
+
701
+/*
702
+ * The VMA has been dup'd, initialize the vm_private_data with a new tracking
703
+ * struct
704
+ */
705
+static void rdma_umap_open(struct vm_area_struct *vma)
706
+{
707
+ struct ib_uverbs_file *ufile = vma->vm_file->private_data;
708
+ struct rdma_umap_priv *opriv = vma->vm_private_data;
709
+ struct rdma_umap_priv *priv;
710
+
711
+ if (!opriv)
712
+ return;
713
+
714
+ /* We are racing with disassociation */
715
+ if (!down_read_trylock(&ufile->hw_destroy_rwsem))
716
+ goto out_zap;
717
+ /*
718
+ * Disassociation already completed, the VMA should already be zapped.
719
+ */
720
+ if (!ufile->ucontext)
721
+ goto out_unlock;
722
+
723
+ priv = kzalloc(sizeof(*priv), GFP_KERNEL);
724
+ if (!priv)
725
+ goto out_unlock;
726
+ rdma_umap_priv_init(priv, vma, opriv->entry);
727
+
728
+ up_read(&ufile->hw_destroy_rwsem);
729
+ return;
730
+
731
+out_unlock:
732
+ up_read(&ufile->hw_destroy_rwsem);
733
+out_zap:
734
+ /*
735
+ * We can't allow the VMA to be created with the actual IO pages, that
736
+ * would break our API contract, and it can't be stopped at this
737
+ * point, so zap it.
738
+ */
739
+ vma->vm_private_data = NULL;
740
+ zap_vma_ptes(vma, vma->vm_start, vma->vm_end - vma->vm_start);
741
+}
742
+
743
+static void rdma_umap_close(struct vm_area_struct *vma)
744
+{
745
+ struct ib_uverbs_file *ufile = vma->vm_file->private_data;
746
+ struct rdma_umap_priv *priv = vma->vm_private_data;
747
+
748
+ if (!priv)
749
+ return;
750
+
751
+ /*
752
+ * The vma holds a reference on the struct file that created it, which
753
+ * in turn means that the ib_uverbs_file is guaranteed to exist at
754
+ * this point.
755
+ */
756
+ mutex_lock(&ufile->umap_lock);
757
+ if (priv->entry)
758
+ rdma_user_mmap_entry_put(priv->entry);
759
+
760
+ list_del(&priv->list);
761
+ mutex_unlock(&ufile->umap_lock);
762
+ kfree(priv);
763
+}
764
+
765
+/*
766
+ * Once the zap_vma_ptes has been called touches to the VMA will come here and
767
+ * we return a dummy writable zero page for all the pfns.
768
+ */
769
+static vm_fault_t rdma_umap_fault(struct vm_fault *vmf)
770
+{
771
+ struct ib_uverbs_file *ufile = vmf->vma->vm_file->private_data;
772
+ struct rdma_umap_priv *priv = vmf->vma->vm_private_data;
773
+ vm_fault_t ret = 0;
774
+
775
+ if (!priv)
776
+ return VM_FAULT_SIGBUS;
777
+
778
+ /* Read only pages can just use the system zero page. */
779
+ if (!(vmf->vma->vm_flags & (VM_WRITE | VM_MAYWRITE))) {
780
+ vmf->page = ZERO_PAGE(vmf->address);
781
+ get_page(vmf->page);
782
+ return 0;
783
+ }
784
+
785
+ mutex_lock(&ufile->umap_lock);
786
+ if (!ufile->disassociate_page)
787
+ ufile->disassociate_page =
788
+ alloc_pages(vmf->gfp_mask | __GFP_ZERO, 0);
789
+
790
+ if (ufile->disassociate_page) {
791
+ /*
792
+ * This VMA is forced to always be shared so this doesn't have
793
+ * to worry about COW.
794
+ */
795
+ vmf->page = ufile->disassociate_page;
796
+ get_page(vmf->page);
797
+ } else {
798
+ ret = VM_FAULT_SIGBUS;
799
+ }
800
+ mutex_unlock(&ufile->umap_lock);
801
+
802
+ return ret;
803
+}
804
+
805
+static const struct vm_operations_struct rdma_umap_ops = {
806
+ .open = rdma_umap_open,
807
+ .close = rdma_umap_close,
808
+ .fault = rdma_umap_fault,
809
+};
810
+
811
+void uverbs_user_mmap_disassociate(struct ib_uverbs_file *ufile)
812
+{
813
+ struct rdma_umap_priv *priv, *next_priv;
814
+
815
+ lockdep_assert_held(&ufile->hw_destroy_rwsem);
816
+
817
+ while (1) {
818
+ struct mm_struct *mm = NULL;
819
+
820
+ /* Get an arbitrary mm pointer that hasn't been cleaned yet */
821
+ mutex_lock(&ufile->umap_lock);
822
+ while (!list_empty(&ufile->umaps)) {
823
+ int ret;
824
+
825
+ priv = list_first_entry(&ufile->umaps,
826
+ struct rdma_umap_priv, list);
827
+ mm = priv->vma->vm_mm;
828
+ ret = mmget_not_zero(mm);
829
+ if (!ret) {
830
+ list_del_init(&priv->list);
831
+ if (priv->entry) {
832
+ rdma_user_mmap_entry_put(priv->entry);
833
+ priv->entry = NULL;
834
+ }
835
+ mm = NULL;
836
+ continue;
837
+ }
838
+ break;
839
+ }
840
+ mutex_unlock(&ufile->umap_lock);
841
+ if (!mm)
842
+ return;
843
+
844
+ /*
845
+ * The umap_lock is nested under mmap_lock since it used within
846
+ * the vma_ops callbacks, so we have to clean the list one mm
847
+ * at a time to get the lock ordering right. Typically there
848
+ * will only be one mm, so no big deal.
849
+ */
850
+ mmap_read_lock(mm);
851
+ mutex_lock(&ufile->umap_lock);
852
+ list_for_each_entry_safe (priv, next_priv, &ufile->umaps,
853
+ list) {
854
+ struct vm_area_struct *vma = priv->vma;
855
+
856
+ if (vma->vm_mm != mm)
857
+ continue;
858
+ list_del_init(&priv->list);
859
+
860
+ zap_vma_ptes(vma, vma->vm_start,
861
+ vma->vm_end - vma->vm_start);
862
+
863
+ if (priv->entry) {
864
+ rdma_user_mmap_entry_put(priv->entry);
865
+ priv->entry = NULL;
866
+ }
867
+ }
868
+ mutex_unlock(&ufile->umap_lock);
869
+ mmap_read_unlock(mm);
870
+ mmput(mm);
871
+ }
818872 }
819873
820874 /*
....@@ -840,6 +894,7 @@
840894 if (!atomic_inc_not_zero(&dev->refcount))
841895 return -ENXIO;
842896
897
+ get_device(&dev->dev);
843898 srcu_key = srcu_read_lock(&dev->disassociate_srcu);
844899 mutex_lock(&dev->lists_mutex);
845900 ib_dev = srcu_dereference(dev->ib_dev,
....@@ -849,13 +904,18 @@
849904 goto err;
850905 }
851906
907
+ if (!rdma_dev_access_netns(ib_dev, current->nsproxy->net_ns)) {
908
+ ret = -EPERM;
909
+ goto err;
910
+ }
911
+
852912 /* In case IB device supports disassociate ucontext, there is no hard
853913 * dependency between uverbs device and its low level device.
854914 */
855
- module_dependent = !(ib_dev->disassociate_ucontext);
915
+ module_dependent = !(ib_dev->ops.disassociate_ucontext);
856916
857917 if (module_dependent) {
858
- if (!try_module_get(ib_dev->owner)) {
918
+ if (!try_module_get(ib_dev->ops.owner)) {
859919 ret = -ENODEV;
860920 goto err;
861921 }
....@@ -877,22 +937,20 @@
877937 spin_lock_init(&file->uobjects_lock);
878938 INIT_LIST_HEAD(&file->uobjects);
879939 init_rwsem(&file->hw_destroy_rwsem);
940
+ mutex_init(&file->umap_lock);
941
+ INIT_LIST_HEAD(&file->umaps);
880942
881943 filp->private_data = file;
882
- kobject_get(&dev->kobj);
883944 list_add_tail(&file->list, &dev->uverbs_file_list);
884945 mutex_unlock(&dev->lists_mutex);
885946 srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
886947
887
- file->uverbs_cmd_mask = ib_dev->uverbs_cmd_mask;
888
- file->uverbs_ex_cmd_mask = ib_dev->uverbs_ex_cmd_mask;
889
-
890948 setup_ufile_idr_uobject(file);
891949
892
- return nonseekable_open(inode, filp);
950
+ return stream_open(inode, filp);
893951
894952 err_module:
895
- module_put(ib_dev->owner);
953
+ module_put(ib_dev->ops.owner);
896954
897955 err:
898956 mutex_unlock(&dev->lists_mutex);
....@@ -900,6 +958,7 @@
900958 if (atomic_dec_and_test(&dev->refcount))
901959 ib_uverbs_comp_dev(dev);
902960
961
+ put_device(&dev->dev);
903962 return ret;
904963 }
905964
....@@ -910,10 +969,7 @@
910969 uverbs_destroy_ufile_hw(file, RDMA_REMOVE_CLOSE);
911970
912971 mutex_lock(&file->device->lists_mutex);
913
- if (!file->is_closed) {
914
- list_del(&file->list);
915
- file->is_closed = 1;
916
- }
972
+ list_del_init(&file->list);
917973 mutex_unlock(&file->device->lists_mutex);
918974
919975 kref_put(&file->ref, ib_uverbs_release_file);
....@@ -928,7 +984,7 @@
928984 .release = ib_uverbs_close,
929985 .llseek = no_llseek,
930986 .unlocked_ioctl = ib_uverbs_ioctl,
931
- .compat_ioctl = ib_uverbs_ioctl,
987
+ .compat_ioctl = compat_ptr_ioctl,
932988 };
933989
934990 static const struct file_operations uverbs_mmap_fops = {
....@@ -939,55 +995,92 @@
939995 .release = ib_uverbs_close,
940996 .llseek = no_llseek,
941997 .unlocked_ioctl = ib_uverbs_ioctl,
942
- .compat_ioctl = ib_uverbs_ioctl,
998
+ .compat_ioctl = compat_ptr_ioctl,
943999 };
1000
+
1001
+static int ib_uverbs_get_nl_info(struct ib_device *ibdev, void *client_data,
1002
+ struct ib_client_nl_info *res)
1003
+{
1004
+ struct ib_uverbs_device *uverbs_dev = client_data;
1005
+ int ret;
1006
+
1007
+ if (res->port != -1)
1008
+ return -EINVAL;
1009
+
1010
+ res->abi = ibdev->ops.uverbs_abi_ver;
1011
+ res->cdev = &uverbs_dev->dev;
1012
+
1013
+ /*
1014
+ * To support DRIVER_ID binding in userspace some of the driver need
1015
+ * upgrading to expose their PCI dependent revision information
1016
+ * through get_context instead of relying on modalias matching. When
1017
+ * the drivers are fixed they can drop this flag.
1018
+ */
1019
+ if (!ibdev->ops.uverbs_no_driver_id_binding) {
1020
+ ret = nla_put_u32(res->nl_msg, RDMA_NLDEV_ATTR_UVERBS_DRIVER_ID,
1021
+ ibdev->ops.driver_id);
1022
+ if (ret)
1023
+ return ret;
1024
+ }
1025
+ return 0;
1026
+}
9441027
9451028 static struct ib_client uverbs_client = {
9461029 .name = "uverbs",
1030
+ .no_kverbs_req = true,
9471031 .add = ib_uverbs_add_one,
948
- .remove = ib_uverbs_remove_one
1032
+ .remove = ib_uverbs_remove_one,
1033
+ .get_nl_info = ib_uverbs_get_nl_info,
9491034 };
1035
+MODULE_ALIAS_RDMA_CLIENT("uverbs");
9501036
951
-static ssize_t show_ibdev(struct device *device, struct device_attribute *attr,
1037
+static ssize_t ibdev_show(struct device *device, struct device_attribute *attr,
9521038 char *buf)
9531039 {
1040
+ struct ib_uverbs_device *dev =
1041
+ container_of(device, struct ib_uverbs_device, dev);
9541042 int ret = -ENODEV;
9551043 int srcu_key;
956
- struct ib_uverbs_device *dev = dev_get_drvdata(device);
9571044 struct ib_device *ib_dev;
958
-
959
- if (!dev)
960
- return -ENODEV;
9611045
9621046 srcu_key = srcu_read_lock(&dev->disassociate_srcu);
9631047 ib_dev = srcu_dereference(dev->ib_dev, &dev->disassociate_srcu);
9641048 if (ib_dev)
965
- ret = sprintf(buf, "%s\n", ib_dev->name);
1049
+ ret = sprintf(buf, "%s\n", dev_name(&ib_dev->dev));
9661050 srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
9671051
9681052 return ret;
9691053 }
970
-static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
1054
+static DEVICE_ATTR_RO(ibdev);
9711055
972
-static ssize_t show_dev_abi_version(struct device *device,
973
- struct device_attribute *attr, char *buf)
1056
+static ssize_t abi_version_show(struct device *device,
1057
+ struct device_attribute *attr, char *buf)
9741058 {
975
- struct ib_uverbs_device *dev = dev_get_drvdata(device);
1059
+ struct ib_uverbs_device *dev =
1060
+ container_of(device, struct ib_uverbs_device, dev);
9761061 int ret = -ENODEV;
9771062 int srcu_key;
9781063 struct ib_device *ib_dev;
9791064
980
- if (!dev)
981
- return -ENODEV;
9821065 srcu_key = srcu_read_lock(&dev->disassociate_srcu);
9831066 ib_dev = srcu_dereference(dev->ib_dev, &dev->disassociate_srcu);
9841067 if (ib_dev)
985
- ret = sprintf(buf, "%d\n", ib_dev->uverbs_abi_ver);
1068
+ ret = sprintf(buf, "%u\n", ib_dev->ops.uverbs_abi_ver);
9861069 srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
9871070
9881071 return ret;
9891072 }
990
-static DEVICE_ATTR(abi_version, S_IRUGO, show_dev_abi_version, NULL);
1073
+static DEVICE_ATTR_RO(abi_version);
1074
+
1075
+static struct attribute *ib_dev_attrs[] = {
1076
+ &dev_attr_abi_version.attr,
1077
+ &dev_attr_ibdev.attr,
1078
+ NULL,
1079
+};
1080
+
1081
+static const struct attribute_group dev_attr_group = {
1082
+ .attrs = ib_dev_attrs,
1083
+};
9911084
9921085 static CLASS_ATTR_STRING(abi_version, S_IRUGO,
9931086 __stringify(IB_USER_VERBS_ABI_VERSION));
....@@ -997,7 +1090,7 @@
9971090 {
9981091 struct uverbs_api *uapi;
9991092
1000
- uapi = uverbs_alloc_api(device->driver_specs, device->driver_id);
1093
+ uapi = uverbs_alloc_api(device);
10011094 if (IS_ERR(uapi))
10021095 return PTR_ERR(uapi);
10031096
....@@ -1005,107 +1098,94 @@
10051098 return 0;
10061099 }
10071100
1008
-static void ib_uverbs_add_one(struct ib_device *device)
1101
+static int ib_uverbs_add_one(struct ib_device *device)
10091102 {
10101103 int devnum;
10111104 dev_t base;
10121105 struct ib_uverbs_device *uverbs_dev;
10131106 int ret;
10141107
1015
- if (!device->alloc_ucontext)
1016
- return;
1108
+ if (!device->ops.alloc_ucontext)
1109
+ return -EOPNOTSUPP;
10171110
10181111 uverbs_dev = kzalloc(sizeof(*uverbs_dev), GFP_KERNEL);
10191112 if (!uverbs_dev)
1020
- return;
1113
+ return -ENOMEM;
10211114
10221115 ret = init_srcu_struct(&uverbs_dev->disassociate_srcu);
10231116 if (ret) {
10241117 kfree(uverbs_dev);
1025
- return;
1118
+ return -ENOMEM;
10261119 }
10271120
1121
+ device_initialize(&uverbs_dev->dev);
1122
+ uverbs_dev->dev.class = uverbs_class;
1123
+ uverbs_dev->dev.parent = device->dev.parent;
1124
+ uverbs_dev->dev.release = ib_uverbs_release_dev;
1125
+ uverbs_dev->groups[0] = &dev_attr_group;
1126
+ uverbs_dev->dev.groups = uverbs_dev->groups;
10281127 atomic_set(&uverbs_dev->refcount, 1);
10291128 init_completion(&uverbs_dev->comp);
10301129 uverbs_dev->xrcd_tree = RB_ROOT;
10311130 mutex_init(&uverbs_dev->xrcd_tree_mutex);
1032
- kobject_init(&uverbs_dev->kobj, &ib_uverbs_dev_ktype);
10331131 mutex_init(&uverbs_dev->lists_mutex);
10341132 INIT_LIST_HEAD(&uverbs_dev->uverbs_file_list);
1035
- INIT_LIST_HEAD(&uverbs_dev->uverbs_events_file_list);
1133
+ rcu_assign_pointer(uverbs_dev->ib_dev, device);
1134
+ uverbs_dev->num_comp_vectors = device->num_comp_vectors;
10361135
1037
- devnum = find_first_zero_bit(dev_map, IB_UVERBS_MAX_DEVICES);
1038
- if (devnum >= IB_UVERBS_MAX_DEVICES)
1136
+ devnum = ida_alloc_max(&uverbs_ida, IB_UVERBS_MAX_DEVICES - 1,
1137
+ GFP_KERNEL);
1138
+ if (devnum < 0) {
1139
+ ret = -ENOMEM;
10391140 goto err;
1141
+ }
10401142 uverbs_dev->devnum = devnum;
1041
- set_bit(devnum, dev_map);
10421143 if (devnum >= IB_UVERBS_NUM_FIXED_MINOR)
10431144 base = dynamic_uverbs_dev + devnum - IB_UVERBS_NUM_FIXED_MINOR;
10441145 else
10451146 base = IB_UVERBS_BASE_DEV + devnum;
10461147
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))
1148
+ ret = ib_uverbs_create_uapi(device, uverbs_dev);
1149
+ if (ret)
10511150 goto err_uapi;
10521151
1053
- cdev_init(&uverbs_dev->cdev, NULL);
1152
+ uverbs_dev->dev.devt = base;
1153
+ dev_set_name(&uverbs_dev->dev, "uverbs%d", uverbs_dev->devnum);
1154
+
1155
+ cdev_init(&uverbs_dev->cdev,
1156
+ device->ops.mmap ? &uverbs_mmap_fops : &uverbs_fops);
10541157 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;
10601158
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;
1159
+ ret = cdev_device_add(&uverbs_dev->cdev, &uverbs_dev->dev);
1160
+ if (ret)
1161
+ goto err_uapi;
10711162
10721163 ib_set_client_data(device, &uverbs_client, uverbs_dev);
1164
+ return 0;
10731165
1074
- return;
1075
-
1076
-err_class:
1077
- device_destroy(uverbs_class, uverbs_dev->cdev.dev);
1078
-err_cdev:
1079
- cdev_del(&uverbs_dev->cdev);
10801166 err_uapi:
1081
- clear_bit(devnum, dev_map);
1167
+ ida_free(&uverbs_ida, devnum);
10821168 err:
10831169 if (atomic_dec_and_test(&uverbs_dev->refcount))
10841170 ib_uverbs_comp_dev(uverbs_dev);
10851171 wait_for_completion(&uverbs_dev->comp);
1086
- kobject_put(&uverbs_dev->kobj);
1087
- return;
1172
+ put_device(&uverbs_dev->dev);
1173
+ return ret;
10881174 }
10891175
10901176 static void ib_uverbs_free_hw_resources(struct ib_uverbs_device *uverbs_dev,
10911177 struct ib_device *ib_dev)
10921178 {
10931179 struct ib_uverbs_file *file;
1094
- struct ib_uverbs_async_event_file *event_file;
1095
- struct ib_event event;
10961180
10971181 /* Pending running commands to terminate */
10981182 uverbs_disassociate_api_pre(uverbs_dev);
1099
- event.event = IB_EVENT_DEVICE_FATAL;
1100
- event.element.port_num = 0;
1101
- event.device = ib_dev;
11021183
11031184 mutex_lock(&uverbs_dev->lists_mutex);
11041185 while (!list_empty(&uverbs_dev->uverbs_file_list)) {
11051186 file = list_first_entry(&uverbs_dev->uverbs_file_list,
11061187 struct ib_uverbs_file, list);
1107
- file->is_closed = 1;
1108
- list_del(&file->list);
1188
+ list_del_init(&file->list);
11091189 kref_get(&file->ref);
11101190
11111191 /* We must release the mutex before going ahead and calling
....@@ -1115,30 +1195,10 @@
11151195 */
11161196 mutex_unlock(&uverbs_dev->lists_mutex);
11171197
1118
- ib_uverbs_event_handler(&file->event_handler, &event);
11191198 uverbs_destroy_ufile_hw(file, RDMA_REMOVE_DRIVER_REMOVE);
11201199 kref_put(&file->ref, ib_uverbs_release_file);
11211200
11221201 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);
11421202 }
11431203 mutex_unlock(&uverbs_dev->lists_mutex);
11441204
....@@ -1150,15 +1210,10 @@
11501210 struct ib_uverbs_device *uverbs_dev = client_data;
11511211 int wait_clients = 1;
11521212
1153
- if (!uverbs_dev)
1154
- return;
1213
+ cdev_device_del(&uverbs_dev->cdev, &uverbs_dev->dev);
1214
+ ida_free(&uverbs_ida, uverbs_dev->devnum);
11551215
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) {
1216
+ if (device->ops.disassociate_ucontext) {
11621217 /* We disassociate HW resources and immediately return.
11631218 * Userspace will see a EIO errno for all future access.
11641219 * Upon returning, ib_device may be freed internally and is not
....@@ -1179,7 +1234,7 @@
11791234 if (wait_clients)
11801235 wait_for_completion(&uverbs_dev->comp);
11811236
1182
- kobject_put(&uverbs_dev->kobj);
1237
+ put_device(&uverbs_dev->dev);
11831238 }
11841239
11851240 static char *uverbs_devnode(struct device *dev, umode_t *mode)
....@@ -1255,6 +1310,7 @@
12551310 IB_UVERBS_NUM_FIXED_MINOR);
12561311 unregister_chrdev_region(dynamic_uverbs_dev,
12571312 IB_UVERBS_NUM_DYNAMIC_MINOR);
1313
+ mmu_notifier_synchronize();
12581314 }
12591315
12601316 module_init(ib_uverbs_init);