.. | .. |
---|
45 | 45 | #include <linux/cdev.h> |
---|
46 | 46 | #include <linux/anon_inodes.h> |
---|
47 | 47 | #include <linux/slab.h> |
---|
| 48 | +#include <linux/sched/mm.h> |
---|
48 | 49 | |
---|
49 | 50 | #include <linux/uaccess.h> |
---|
50 | 51 | |
---|
51 | 52 | #include <rdma/ib.h> |
---|
52 | 53 | #include <rdma/uverbs_std_types.h> |
---|
| 54 | +#include <rdma/rdma_netlink.h> |
---|
53 | 55 | |
---|
54 | 56 | #include "uverbs.h" |
---|
55 | 57 | #include "core_priv.h" |
---|
.. | .. |
---|
72 | 74 | static dev_t dynamic_uverbs_dev; |
---|
73 | 75 | static struct class *uverbs_class; |
---|
74 | 76 | |
---|
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); |
---|
135 | 79 | static void ib_uverbs_remove_one(struct ib_device *device, void *client_data); |
---|
136 | 80 | |
---|
137 | 81 | /* |
---|
138 | 82 | * Must be called with the ufile->device->disassociate_srcu held, and the lock |
---|
139 | 83 | * must be held until use of the ucontext is finished. |
---|
140 | 84 | */ |
---|
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) |
---|
142 | 86 | { |
---|
143 | 87 | /* |
---|
144 | 88 | * We do not hold the hw_destroy_rwsem lock for this flow, instead |
---|
.. | .. |
---|
156 | 100 | |
---|
157 | 101 | return ucontext; |
---|
158 | 102 | } |
---|
159 | | -EXPORT_SYMBOL(ib_uverbs_get_ucontext); |
---|
| 103 | +EXPORT_SYMBOL(ib_uverbs_get_ucontext_file); |
---|
160 | 104 | |
---|
161 | 105 | int uverbs_dealloc_mw(struct ib_mw *mw) |
---|
162 | 106 | { |
---|
163 | 107 | struct ib_pd *pd = mw->pd; |
---|
164 | 108 | int ret; |
---|
165 | 109 | |
---|
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); |
---|
169 | 116 | return ret; |
---|
170 | 117 | } |
---|
171 | 118 | |
---|
172 | | -static void ib_uverbs_release_dev(struct kobject *kobj) |
---|
| 119 | +static void ib_uverbs_release_dev(struct device *device) |
---|
173 | 120 | { |
---|
174 | 121 | struct ib_uverbs_device *dev = |
---|
175 | | - container_of(kobj, struct ib_uverbs_device, kobj); |
---|
| 122 | + container_of(device, struct ib_uverbs_device, dev); |
---|
176 | 123 | |
---|
177 | 124 | uverbs_destroy_api(dev->uapi); |
---|
178 | 125 | cleanup_srcu_struct(&dev->disassociate_srcu); |
---|
| 126 | + mutex_destroy(&dev->lists_mutex); |
---|
| 127 | + mutex_destroy(&dev->xrcd_tree_mutex); |
---|
179 | 128 | kfree(dev); |
---|
180 | 129 | } |
---|
181 | 130 | |
---|
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) |
---|
197 | 133 | { |
---|
198 | 134 | struct ib_uverbs_event *evt, *tmp; |
---|
199 | 135 | |
---|
.. | .. |
---|
208 | 144 | uverbs_uobject_put(&ev_file->uobj); |
---|
209 | 145 | } |
---|
210 | 146 | |
---|
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); |
---|
217 | 148 | } |
---|
218 | 149 | |
---|
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) |
---|
221 | 151 | { |
---|
| 152 | + struct ib_uverbs_async_event_file *async_file = uobj->event_file; |
---|
222 | 153 | struct ib_uverbs_event *evt, *tmp; |
---|
223 | 154 | |
---|
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); |
---|
225 | 159 | list_for_each_entry_safe(evt, tmp, &uobj->event_list, obj_list) { |
---|
226 | 160 | list_del(&evt->list); |
---|
227 | 161 | kfree(evt); |
---|
228 | 162 | } |
---|
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); |
---|
230 | 165 | } |
---|
231 | 166 | |
---|
232 | 167 | void ib_uverbs_detach_umcast(struct ib_qp *qp, |
---|
.. | .. |
---|
258 | 193 | srcu_key = srcu_read_lock(&file->device->disassociate_srcu); |
---|
259 | 194 | ib_dev = srcu_dereference(file->device->ib_dev, |
---|
260 | 195 | &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); |
---|
263 | 198 | srcu_read_unlock(&file->device->disassociate_srcu, srcu_key); |
---|
264 | 199 | |
---|
265 | 200 | if (atomic_dec_and_test(&file->device->refcount)) |
---|
266 | 201 | ib_uverbs_comp_dev(file->device); |
---|
267 | 202 | |
---|
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); |
---|
272 | 211 | kfree(file); |
---|
273 | 212 | } |
---|
274 | 213 | |
---|
.. | .. |
---|
399 | 338 | return fasync_helper(fd, filp, on, &comp_ev_file->ev_queue.async_queue); |
---|
400 | 339 | } |
---|
401 | 340 | |
---|
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 | | - |
---|
452 | 341 | const struct file_operations uverbs_event_fops = { |
---|
453 | 342 | .owner = THIS_MODULE, |
---|
454 | 343 | .read = ib_uverbs_comp_event_read, |
---|
455 | 344 | .poll = ib_uverbs_comp_event_poll, |
---|
456 | | - .release = ib_uverbs_comp_event_close, |
---|
| 345 | + .release = uverbs_uobject_fd_release, |
---|
457 | 346 | .fasync = ib_uverbs_comp_event_fasync, |
---|
458 | 347 | .llseek = no_llseek, |
---|
459 | 348 | }; |
---|
460 | 349 | |
---|
461 | | -static const struct file_operations uverbs_async_event_fops = { |
---|
| 350 | +const struct file_operations uverbs_async_event_fops = { |
---|
462 | 351 | .owner = THIS_MODULE, |
---|
463 | 352 | .read = ib_uverbs_async_event_read, |
---|
464 | 353 | .poll = ib_uverbs_async_event_poll, |
---|
465 | | - .release = ib_uverbs_async_event_close, |
---|
| 354 | + .release = uverbs_async_event_release, |
---|
466 | 355 | .fasync = ib_uverbs_async_event_fasync, |
---|
467 | 356 | .llseek = no_llseek, |
---|
468 | 357 | }; |
---|
.. | .. |
---|
489 | 378 | return; |
---|
490 | 379 | } |
---|
491 | 380 | |
---|
492 | | - uobj = container_of(cq->uobject, struct ib_ucq_object, uobject); |
---|
| 381 | + uobj = cq->uobject; |
---|
493 | 382 | |
---|
494 | | - entry->desc.comp.cq_handle = cq->uobject->user_handle; |
---|
| 383 | + entry->desc.comp.cq_handle = cq->uobject->uevent.uobject.user_handle; |
---|
495 | 384 | entry->counter = &uobj->comp_events_reported; |
---|
496 | 385 | |
---|
497 | 386 | list_add_tail(&entry->list, &ev_queue->event_list); |
---|
.. | .. |
---|
502 | 391 | kill_fasync(&ev_queue->async_queue, SIGIO, POLL_IN); |
---|
503 | 392 | } |
---|
504 | 393 | |
---|
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) |
---|
509 | 397 | { |
---|
510 | 398 | struct ib_uverbs_event *entry; |
---|
511 | 399 | unsigned long flags; |
---|
512 | 400 | |
---|
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); |
---|
516 | 407 | return; |
---|
517 | 408 | } |
---|
518 | 409 | |
---|
519 | 410 | entry = kmalloc(sizeof(*entry), GFP_ATOMIC); |
---|
520 | 411 | if (!entry) { |
---|
521 | | - spin_unlock_irqrestore(&file->async_file->ev_queue.lock, flags); |
---|
| 412 | + spin_unlock_irqrestore(&async_file->ev_queue.lock, flags); |
---|
522 | 413 | return; |
---|
523 | 414 | } |
---|
524 | 415 | |
---|
525 | | - entry->desc.async.element = element; |
---|
| 416 | + entry->desc.async.element = element; |
---|
526 | 417 | 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; |
---|
529 | 420 | |
---|
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); |
---|
531 | 422 | if (obj_list) |
---|
532 | 423 | 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); |
---|
534 | 425 | |
---|
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); |
---|
537 | 436 | } |
---|
538 | 437 | |
---|
539 | 438 | void ib_uverbs_cq_event_handler(struct ib_event *event, void *context_ptr) |
---|
540 | 439 | { |
---|
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); |
---|
547 | 441 | } |
---|
548 | 442 | |
---|
549 | 443 | void ib_uverbs_qp_event_handler(struct ib_event *event, void *context_ptr) |
---|
550 | 444 | { |
---|
551 | | - struct ib_uevent_object *uobj; |
---|
552 | | - |
---|
553 | 445 | /* for XRC target qp's, check that qp is live */ |
---|
554 | 446 | if (!event->element.qp->uobject) |
---|
555 | 447 | return; |
---|
556 | 448 | |
---|
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); |
---|
563 | 450 | } |
---|
564 | 451 | |
---|
565 | 452 | void ib_uverbs_wq_event_handler(struct ib_event *event, void *context_ptr) |
---|
566 | 453 | { |
---|
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); |
---|
573 | 455 | } |
---|
574 | 456 | |
---|
575 | 457 | void ib_uverbs_srq_event_handler(struct ib_event *event, void *context_ptr) |
---|
576 | 458 | { |
---|
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); |
---|
585 | 460 | } |
---|
586 | 461 | |
---|
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) |
---|
589 | 464 | { |
---|
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); |
---|
601 | 469 | } |
---|
602 | 470 | |
---|
603 | 471 | void ib_uverbs_init_event_queue(struct ib_uverbs_event_queue *ev_queue) |
---|
.. | .. |
---|
609 | 477 | ev_queue->async_queue = NULL; |
---|
610 | 478 | } |
---|
611 | 479 | |
---|
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) |
---|
614 | 482 | { |
---|
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; |
---|
617 | 485 | |
---|
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); |
---|
621 | 487 | |
---|
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); |
---|
630 | 496 | |
---|
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, |
---|
641 | 498 | 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); |
---|
686 | 500 | } |
---|
687 | 501 | |
---|
688 | 502 | 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) |
---|
691 | 505 | { |
---|
692 | | - if (extended) { |
---|
| 506 | + if (method_elm->is_ex) { |
---|
693 | 507 | count -= sizeof(*hdr) + sizeof(*ex_hdr); |
---|
694 | 508 | |
---|
695 | 509 | if ((hdr->in_words + ex_hdr->provider_in_words) * 8 != count) |
---|
696 | 510 | return -EINVAL; |
---|
| 511 | + |
---|
| 512 | + if (hdr->in_words * 8 < method_elm->req_size) |
---|
| 513 | + return -ENOSPC; |
---|
697 | 514 | |
---|
698 | 515 | if (ex_hdr->cmd_hdr_reserved) |
---|
699 | 516 | return -EINVAL; |
---|
.. | .. |
---|
702 | 519 | if (!hdr->out_words && !ex_hdr->provider_out_words) |
---|
703 | 520 | return -EINVAL; |
---|
704 | 521 | |
---|
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), |
---|
707 | 526 | (hdr->out_words + ex_hdr->provider_out_words) * 8)) |
---|
708 | 527 | return -EFAULT; |
---|
709 | 528 | } else { |
---|
.. | .. |
---|
718 | 537 | if (hdr->in_words * 4 != count) |
---|
719 | 538 | return -EINVAL; |
---|
720 | 539 | |
---|
| 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 | + |
---|
721 | 558 | return 0; |
---|
722 | 559 | } |
---|
723 | 560 | |
---|
.. | .. |
---|
725 | 562 | size_t count, loff_t *pos) |
---|
726 | 563 | { |
---|
727 | 564 | 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; |
---|
728 | 567 | struct ib_uverbs_ex_cmd_hdr ex_hdr; |
---|
729 | 568 | struct ib_uverbs_cmd_hdr hdr; |
---|
730 | | - bool extended; |
---|
| 569 | + struct uverbs_attr_bundle bundle; |
---|
731 | 570 | int srcu_key; |
---|
732 | | - u32 command; |
---|
733 | 571 | ssize_t ret; |
---|
734 | 572 | |
---|
735 | 573 | if (!ib_safe_file_access(filp)) { |
---|
.. | .. |
---|
744 | 582 | if (copy_from_user(&hdr, buf, sizeof(hdr))) |
---|
745 | 583 | return -EFAULT; |
---|
746 | 584 | |
---|
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); |
---|
750 | 588 | |
---|
751 | | - if (extended) { |
---|
| 589 | + if (method_elm->is_ex) { |
---|
752 | 590 | if (count < (sizeof(hdr) + sizeof(ex_hdr))) |
---|
753 | 591 | return -EINVAL; |
---|
754 | 592 | if (copy_from_user(&ex_hdr, buf + sizeof(hdr), sizeof(ex_hdr))) |
---|
755 | 593 | return -EFAULT; |
---|
756 | 594 | } |
---|
757 | 595 | |
---|
758 | | - ret = verify_hdr(&hdr, &ex_hdr, count, extended); |
---|
| 596 | + ret = verify_hdr(&hdr, &ex_hdr, count, method_elm); |
---|
759 | 597 | if (ret) |
---|
760 | 598 | return ret; |
---|
761 | 599 | |
---|
762 | 600 | srcu_key = srcu_read_lock(&file->device->disassociate_srcu); |
---|
763 | 601 | |
---|
764 | | - if (!verify_command_mask(file, command, extended)) { |
---|
765 | | - ret = -EOPNOTSUPP; |
---|
766 | | - goto out; |
---|
767 | | - } |
---|
768 | | - |
---|
769 | 602 | buf += sizeof(hdr); |
---|
770 | 603 | |
---|
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; |
---|
778 | 612 | |
---|
| 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 { |
---|
779 | 656 | buf += sizeof(ex_hdr); |
---|
780 | 657 | |
---|
781 | | - ib_uverbs_init_udata_buf_or_null(&ucore, buf, |
---|
| 658 | + ib_uverbs_init_udata_buf_or_null(&bundle.ucore, buf, |
---|
782 | 659 | u64_to_user_ptr(ex_hdr.response), |
---|
783 | 660 | hdr.in_words * 8, hdr.out_words * 8); |
---|
784 | 661 | |
---|
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); |
---|
790 | 667 | |
---|
791 | | - ret = uverbs_ex_cmd_table[command](file, &ucore, &uhw); |
---|
792 | | - ret = (ret) ? : count; |
---|
793 | 668 | } |
---|
794 | 669 | |
---|
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: |
---|
796 | 675 | srcu_read_unlock(&file->device->disassociate_srcu, srcu_key); |
---|
797 | | - return ret; |
---|
| 676 | + return (ret) ? : count; |
---|
798 | 677 | } |
---|
| 678 | + |
---|
| 679 | +static const struct vm_operations_struct rdma_umap_ops; |
---|
799 | 680 | |
---|
800 | 681 | static int ib_uverbs_mmap(struct file *filp, struct vm_area_struct *vma) |
---|
801 | 682 | { |
---|
.. | .. |
---|
805 | 686 | int srcu_key; |
---|
806 | 687 | |
---|
807 | 688 | srcu_key = srcu_read_lock(&file->device->disassociate_srcu); |
---|
808 | | - ucontext = ib_uverbs_get_ucontext(file); |
---|
| 689 | + ucontext = ib_uverbs_get_ucontext_file(file); |
---|
809 | 690 | if (IS_ERR(ucontext)) { |
---|
810 | 691 | ret = PTR_ERR(ucontext); |
---|
811 | 692 | goto out; |
---|
812 | 693 | } |
---|
813 | | - |
---|
814 | | - ret = ucontext->device->mmap(ucontext, vma); |
---|
| 694 | + vma->vm_ops = &rdma_umap_ops; |
---|
| 695 | + ret = ucontext->device->ops.mmap(ucontext, vma); |
---|
815 | 696 | out: |
---|
816 | 697 | srcu_read_unlock(&file->device->disassociate_srcu, srcu_key); |
---|
817 | 698 | 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 | + } |
---|
818 | 872 | } |
---|
819 | 873 | |
---|
820 | 874 | /* |
---|
.. | .. |
---|
840 | 894 | if (!atomic_inc_not_zero(&dev->refcount)) |
---|
841 | 895 | return -ENXIO; |
---|
842 | 896 | |
---|
| 897 | + get_device(&dev->dev); |
---|
843 | 898 | srcu_key = srcu_read_lock(&dev->disassociate_srcu); |
---|
844 | 899 | mutex_lock(&dev->lists_mutex); |
---|
845 | 900 | ib_dev = srcu_dereference(dev->ib_dev, |
---|
.. | .. |
---|
849 | 904 | goto err; |
---|
850 | 905 | } |
---|
851 | 906 | |
---|
| 907 | + if (!rdma_dev_access_netns(ib_dev, current->nsproxy->net_ns)) { |
---|
| 908 | + ret = -EPERM; |
---|
| 909 | + goto err; |
---|
| 910 | + } |
---|
| 911 | + |
---|
852 | 912 | /* In case IB device supports disassociate ucontext, there is no hard |
---|
853 | 913 | * dependency between uverbs device and its low level device. |
---|
854 | 914 | */ |
---|
855 | | - module_dependent = !(ib_dev->disassociate_ucontext); |
---|
| 915 | + module_dependent = !(ib_dev->ops.disassociate_ucontext); |
---|
856 | 916 | |
---|
857 | 917 | if (module_dependent) { |
---|
858 | | - if (!try_module_get(ib_dev->owner)) { |
---|
| 918 | + if (!try_module_get(ib_dev->ops.owner)) { |
---|
859 | 919 | ret = -ENODEV; |
---|
860 | 920 | goto err; |
---|
861 | 921 | } |
---|
.. | .. |
---|
877 | 937 | spin_lock_init(&file->uobjects_lock); |
---|
878 | 938 | INIT_LIST_HEAD(&file->uobjects); |
---|
879 | 939 | init_rwsem(&file->hw_destroy_rwsem); |
---|
| 940 | + mutex_init(&file->umap_lock); |
---|
| 941 | + INIT_LIST_HEAD(&file->umaps); |
---|
880 | 942 | |
---|
881 | 943 | filp->private_data = file; |
---|
882 | | - kobject_get(&dev->kobj); |
---|
883 | 944 | list_add_tail(&file->list, &dev->uverbs_file_list); |
---|
884 | 945 | mutex_unlock(&dev->lists_mutex); |
---|
885 | 946 | srcu_read_unlock(&dev->disassociate_srcu, srcu_key); |
---|
886 | 947 | |
---|
887 | | - file->uverbs_cmd_mask = ib_dev->uverbs_cmd_mask; |
---|
888 | | - file->uverbs_ex_cmd_mask = ib_dev->uverbs_ex_cmd_mask; |
---|
889 | | - |
---|
890 | 948 | setup_ufile_idr_uobject(file); |
---|
891 | 949 | |
---|
892 | | - return nonseekable_open(inode, filp); |
---|
| 950 | + return stream_open(inode, filp); |
---|
893 | 951 | |
---|
894 | 952 | err_module: |
---|
895 | | - module_put(ib_dev->owner); |
---|
| 953 | + module_put(ib_dev->ops.owner); |
---|
896 | 954 | |
---|
897 | 955 | err: |
---|
898 | 956 | mutex_unlock(&dev->lists_mutex); |
---|
.. | .. |
---|
900 | 958 | if (atomic_dec_and_test(&dev->refcount)) |
---|
901 | 959 | ib_uverbs_comp_dev(dev); |
---|
902 | 960 | |
---|
| 961 | + put_device(&dev->dev); |
---|
903 | 962 | return ret; |
---|
904 | 963 | } |
---|
905 | 964 | |
---|
.. | .. |
---|
910 | 969 | uverbs_destroy_ufile_hw(file, RDMA_REMOVE_CLOSE); |
---|
911 | 970 | |
---|
912 | 971 | 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); |
---|
917 | 973 | mutex_unlock(&file->device->lists_mutex); |
---|
918 | 974 | |
---|
919 | 975 | kref_put(&file->ref, ib_uverbs_release_file); |
---|
.. | .. |
---|
928 | 984 | .release = ib_uverbs_close, |
---|
929 | 985 | .llseek = no_llseek, |
---|
930 | 986 | .unlocked_ioctl = ib_uverbs_ioctl, |
---|
931 | | - .compat_ioctl = ib_uverbs_ioctl, |
---|
| 987 | + .compat_ioctl = compat_ptr_ioctl, |
---|
932 | 988 | }; |
---|
933 | 989 | |
---|
934 | 990 | static const struct file_operations uverbs_mmap_fops = { |
---|
.. | .. |
---|
939 | 995 | .release = ib_uverbs_close, |
---|
940 | 996 | .llseek = no_llseek, |
---|
941 | 997 | .unlocked_ioctl = ib_uverbs_ioctl, |
---|
942 | | - .compat_ioctl = ib_uverbs_ioctl, |
---|
| 998 | + .compat_ioctl = compat_ptr_ioctl, |
---|
943 | 999 | }; |
---|
| 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 | +} |
---|
944 | 1027 | |
---|
945 | 1028 | static struct ib_client uverbs_client = { |
---|
946 | 1029 | .name = "uverbs", |
---|
| 1030 | + .no_kverbs_req = true, |
---|
947 | 1031 | .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, |
---|
949 | 1034 | }; |
---|
| 1035 | +MODULE_ALIAS_RDMA_CLIENT("uverbs"); |
---|
950 | 1036 | |
---|
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, |
---|
952 | 1038 | char *buf) |
---|
953 | 1039 | { |
---|
| 1040 | + struct ib_uverbs_device *dev = |
---|
| 1041 | + container_of(device, struct ib_uverbs_device, dev); |
---|
954 | 1042 | int ret = -ENODEV; |
---|
955 | 1043 | int srcu_key; |
---|
956 | | - struct ib_uverbs_device *dev = dev_get_drvdata(device); |
---|
957 | 1044 | struct ib_device *ib_dev; |
---|
958 | | - |
---|
959 | | - if (!dev) |
---|
960 | | - return -ENODEV; |
---|
961 | 1045 | |
---|
962 | 1046 | srcu_key = srcu_read_lock(&dev->disassociate_srcu); |
---|
963 | 1047 | ib_dev = srcu_dereference(dev->ib_dev, &dev->disassociate_srcu); |
---|
964 | 1048 | if (ib_dev) |
---|
965 | | - ret = sprintf(buf, "%s\n", ib_dev->name); |
---|
| 1049 | + ret = sprintf(buf, "%s\n", dev_name(&ib_dev->dev)); |
---|
966 | 1050 | srcu_read_unlock(&dev->disassociate_srcu, srcu_key); |
---|
967 | 1051 | |
---|
968 | 1052 | return ret; |
---|
969 | 1053 | } |
---|
970 | | -static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL); |
---|
| 1054 | +static DEVICE_ATTR_RO(ibdev); |
---|
971 | 1055 | |
---|
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) |
---|
974 | 1058 | { |
---|
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); |
---|
976 | 1061 | int ret = -ENODEV; |
---|
977 | 1062 | int srcu_key; |
---|
978 | 1063 | struct ib_device *ib_dev; |
---|
979 | 1064 | |
---|
980 | | - if (!dev) |
---|
981 | | - return -ENODEV; |
---|
982 | 1065 | srcu_key = srcu_read_lock(&dev->disassociate_srcu); |
---|
983 | 1066 | ib_dev = srcu_dereference(dev->ib_dev, &dev->disassociate_srcu); |
---|
984 | 1067 | 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); |
---|
986 | 1069 | srcu_read_unlock(&dev->disassociate_srcu, srcu_key); |
---|
987 | 1070 | |
---|
988 | 1071 | return ret; |
---|
989 | 1072 | } |
---|
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 | +}; |
---|
991 | 1084 | |
---|
992 | 1085 | static CLASS_ATTR_STRING(abi_version, S_IRUGO, |
---|
993 | 1086 | __stringify(IB_USER_VERBS_ABI_VERSION)); |
---|
.. | .. |
---|
997 | 1090 | { |
---|
998 | 1091 | struct uverbs_api *uapi; |
---|
999 | 1092 | |
---|
1000 | | - uapi = uverbs_alloc_api(device->driver_specs, device->driver_id); |
---|
| 1093 | + uapi = uverbs_alloc_api(device); |
---|
1001 | 1094 | if (IS_ERR(uapi)) |
---|
1002 | 1095 | return PTR_ERR(uapi); |
---|
1003 | 1096 | |
---|
.. | .. |
---|
1005 | 1098 | return 0; |
---|
1006 | 1099 | } |
---|
1007 | 1100 | |
---|
1008 | | -static void ib_uverbs_add_one(struct ib_device *device) |
---|
| 1101 | +static int ib_uverbs_add_one(struct ib_device *device) |
---|
1009 | 1102 | { |
---|
1010 | 1103 | int devnum; |
---|
1011 | 1104 | dev_t base; |
---|
1012 | 1105 | struct ib_uverbs_device *uverbs_dev; |
---|
1013 | 1106 | int ret; |
---|
1014 | 1107 | |
---|
1015 | | - if (!device->alloc_ucontext) |
---|
1016 | | - return; |
---|
| 1108 | + if (!device->ops.alloc_ucontext) |
---|
| 1109 | + return -EOPNOTSUPP; |
---|
1017 | 1110 | |
---|
1018 | 1111 | uverbs_dev = kzalloc(sizeof(*uverbs_dev), GFP_KERNEL); |
---|
1019 | 1112 | if (!uverbs_dev) |
---|
1020 | | - return; |
---|
| 1113 | + return -ENOMEM; |
---|
1021 | 1114 | |
---|
1022 | 1115 | ret = init_srcu_struct(&uverbs_dev->disassociate_srcu); |
---|
1023 | 1116 | if (ret) { |
---|
1024 | 1117 | kfree(uverbs_dev); |
---|
1025 | | - return; |
---|
| 1118 | + return -ENOMEM; |
---|
1026 | 1119 | } |
---|
1027 | 1120 | |
---|
| 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; |
---|
1028 | 1127 | atomic_set(&uverbs_dev->refcount, 1); |
---|
1029 | 1128 | init_completion(&uverbs_dev->comp); |
---|
1030 | 1129 | uverbs_dev->xrcd_tree = RB_ROOT; |
---|
1031 | 1130 | mutex_init(&uverbs_dev->xrcd_tree_mutex); |
---|
1032 | | - kobject_init(&uverbs_dev->kobj, &ib_uverbs_dev_ktype); |
---|
1033 | 1131 | mutex_init(&uverbs_dev->lists_mutex); |
---|
1034 | 1132 | 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; |
---|
1036 | 1135 | |
---|
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; |
---|
1039 | 1140 | goto err; |
---|
| 1141 | + } |
---|
1040 | 1142 | uverbs_dev->devnum = devnum; |
---|
1041 | | - set_bit(devnum, dev_map); |
---|
1042 | 1143 | if (devnum >= IB_UVERBS_NUM_FIXED_MINOR) |
---|
1043 | 1144 | base = dynamic_uverbs_dev + devnum - IB_UVERBS_NUM_FIXED_MINOR; |
---|
1044 | 1145 | else |
---|
1045 | 1146 | base = IB_UVERBS_BASE_DEV + devnum; |
---|
1046 | 1147 | |
---|
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) |
---|
1051 | 1150 | goto err_uapi; |
---|
1052 | 1151 | |
---|
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); |
---|
1054 | 1157 | 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; |
---|
1060 | 1158 | |
---|
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; |
---|
1071 | 1162 | |
---|
1072 | 1163 | ib_set_client_data(device, &uverbs_client, uverbs_dev); |
---|
| 1164 | + return 0; |
---|
1073 | 1165 | |
---|
1074 | | - return; |
---|
1075 | | - |
---|
1076 | | -err_class: |
---|
1077 | | - device_destroy(uverbs_class, uverbs_dev->cdev.dev); |
---|
1078 | | -err_cdev: |
---|
1079 | | - cdev_del(&uverbs_dev->cdev); |
---|
1080 | 1166 | err_uapi: |
---|
1081 | | - clear_bit(devnum, dev_map); |
---|
| 1167 | + ida_free(&uverbs_ida, devnum); |
---|
1082 | 1168 | err: |
---|
1083 | 1169 | if (atomic_dec_and_test(&uverbs_dev->refcount)) |
---|
1084 | 1170 | ib_uverbs_comp_dev(uverbs_dev); |
---|
1085 | 1171 | wait_for_completion(&uverbs_dev->comp); |
---|
1086 | | - kobject_put(&uverbs_dev->kobj); |
---|
1087 | | - return; |
---|
| 1172 | + put_device(&uverbs_dev->dev); |
---|
| 1173 | + return ret; |
---|
1088 | 1174 | } |
---|
1089 | 1175 | |
---|
1090 | 1176 | static void ib_uverbs_free_hw_resources(struct ib_uverbs_device *uverbs_dev, |
---|
1091 | 1177 | struct ib_device *ib_dev) |
---|
1092 | 1178 | { |
---|
1093 | 1179 | struct ib_uverbs_file *file; |
---|
1094 | | - struct ib_uverbs_async_event_file *event_file; |
---|
1095 | | - struct ib_event event; |
---|
1096 | 1180 | |
---|
1097 | 1181 | /* Pending running commands to terminate */ |
---|
1098 | 1182 | uverbs_disassociate_api_pre(uverbs_dev); |
---|
1099 | | - event.event = IB_EVENT_DEVICE_FATAL; |
---|
1100 | | - event.element.port_num = 0; |
---|
1101 | | - event.device = ib_dev; |
---|
1102 | 1183 | |
---|
1103 | 1184 | mutex_lock(&uverbs_dev->lists_mutex); |
---|
1104 | 1185 | while (!list_empty(&uverbs_dev->uverbs_file_list)) { |
---|
1105 | 1186 | file = list_first_entry(&uverbs_dev->uverbs_file_list, |
---|
1106 | 1187 | struct ib_uverbs_file, list); |
---|
1107 | | - file->is_closed = 1; |
---|
1108 | | - list_del(&file->list); |
---|
| 1188 | + list_del_init(&file->list); |
---|
1109 | 1189 | kref_get(&file->ref); |
---|
1110 | 1190 | |
---|
1111 | 1191 | /* We must release the mutex before going ahead and calling |
---|
.. | .. |
---|
1115 | 1195 | */ |
---|
1116 | 1196 | mutex_unlock(&uverbs_dev->lists_mutex); |
---|
1117 | 1197 | |
---|
1118 | | - ib_uverbs_event_handler(&file->event_handler, &event); |
---|
1119 | 1198 | uverbs_destroy_ufile_hw(file, RDMA_REMOVE_DRIVER_REMOVE); |
---|
1120 | 1199 | kref_put(&file->ref, ib_uverbs_release_file); |
---|
1121 | 1200 | |
---|
1122 | 1201 | 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); |
---|
1142 | 1202 | } |
---|
1143 | 1203 | mutex_unlock(&uverbs_dev->lists_mutex); |
---|
1144 | 1204 | |
---|
.. | .. |
---|
1150 | 1210 | struct ib_uverbs_device *uverbs_dev = client_data; |
---|
1151 | 1211 | int wait_clients = 1; |
---|
1152 | 1212 | |
---|
1153 | | - if (!uverbs_dev) |
---|
1154 | | - return; |
---|
| 1213 | + cdev_device_del(&uverbs_dev->cdev, &uverbs_dev->dev); |
---|
| 1214 | + ida_free(&uverbs_ida, uverbs_dev->devnum); |
---|
1155 | 1215 | |
---|
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) { |
---|
1162 | 1217 | /* We disassociate HW resources and immediately return. |
---|
1163 | 1218 | * Userspace will see a EIO errno for all future access. |
---|
1164 | 1219 | * Upon returning, ib_device may be freed internally and is not |
---|
.. | .. |
---|
1179 | 1234 | if (wait_clients) |
---|
1180 | 1235 | wait_for_completion(&uverbs_dev->comp); |
---|
1181 | 1236 | |
---|
1182 | | - kobject_put(&uverbs_dev->kobj); |
---|
| 1237 | + put_device(&uverbs_dev->dev); |
---|
1183 | 1238 | } |
---|
1184 | 1239 | |
---|
1185 | 1240 | static char *uverbs_devnode(struct device *dev, umode_t *mode) |
---|
.. | .. |
---|
1255 | 1310 | IB_UVERBS_NUM_FIXED_MINOR); |
---|
1256 | 1311 | unregister_chrdev_region(dynamic_uverbs_dev, |
---|
1257 | 1312 | IB_UVERBS_NUM_DYNAMIC_MINOR); |
---|
| 1313 | + mmu_notifier_synchronize(); |
---|
1258 | 1314 | } |
---|
1259 | 1315 | |
---|
1260 | 1316 | module_init(ib_uverbs_init); |
---|