1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0
3 *
4 * Copyright (c) 2005-2006 Intel Corporation. All rights reserved.
5 *
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
11 *
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
15 *
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer.
19 *
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 * SOFTWARE.
33 */
34
35 #include <sys/cdefs.h>
36 #include <linux/completion.h>
37 #include <linux/file.h>
38 #include <linux/mutex.h>
39 #include <linux/poll.h>
40 #include <linux/sched.h>
41 #include <linux/idr.h>
42 #include <linux/in.h>
43 #include <linux/in6.h>
44 #include <linux/miscdevice.h>
45 #include <linux/slab.h>
46 #include <linux/module.h>
47
48 #include <sys/filio.h>
49
50 #include <rdma/rdma_user_cm.h>
51 #include <rdma/ib_marshall.h>
52 #include <rdma/rdma_cm.h>
53 #include <rdma/rdma_cm_ib.h>
54 #include <rdma/ib_addr.h>
55 #include <rdma/ib.h>
56
57 MODULE_AUTHOR("Sean Hefty");
58 MODULE_DESCRIPTION("RDMA Userspace Connection Manager Access");
59 MODULE_LICENSE("Dual BSD/GPL");
60
61 static unsigned int max_backlog = 1024;
62
63 struct ucma_file {
64 struct mutex mut;
65 struct file *filp;
66 struct list_head ctx_list;
67 struct list_head event_list;
68 wait_queue_head_t poll_wait;
69 struct workqueue_struct *close_wq;
70 };
71
72 struct ucma_context {
73 int id;
74 struct completion comp;
75 atomic_t ref;
76 int events_reported;
77 int backlog;
78
79 struct ucma_file *file;
80 struct rdma_cm_id *cm_id;
81 u64 uid;
82
83 struct list_head list;
84 struct list_head mc_list;
85 /* mark that device is in process of destroying the internal HW
86 * resources, protected by the global mut
87 */
88 int closing;
89 /* sync between removal event and id destroy, protected by file mut */
90 int destroying;
91 struct work_struct close_work;
92 };
93
94 struct ucma_multicast {
95 struct ucma_context *ctx;
96 int id;
97 int events_reported;
98
99 u64 uid;
100 u8 join_state;
101 struct list_head list;
102 struct sockaddr_storage addr;
103 };
104
105 struct ucma_event {
106 struct ucma_context *ctx;
107 struct ucma_multicast *mc;
108 struct list_head list;
109 struct rdma_cm_id *cm_id;
110 struct rdma_ucm_event_resp resp;
111 struct work_struct close_work;
112 };
113
114 static DEFINE_MUTEX(mut);
115 static DEFINE_IDR(ctx_idr);
116 static DEFINE_IDR(multicast_idr);
117
118 static const struct file_operations ucma_fops;
119
_ucma_find_context(int id,struct ucma_file * file)120 static inline struct ucma_context *_ucma_find_context(int id,
121 struct ucma_file *file)
122 {
123 struct ucma_context *ctx;
124
125 ctx = idr_find(&ctx_idr, id);
126 if (!ctx)
127 ctx = ERR_PTR(-ENOENT);
128 else if (ctx->file != file || !ctx->cm_id)
129 ctx = ERR_PTR(-EINVAL);
130 return ctx;
131 }
132
ucma_get_ctx(struct ucma_file * file,int id)133 static struct ucma_context *ucma_get_ctx(struct ucma_file *file, int id)
134 {
135 struct ucma_context *ctx;
136
137 mutex_lock(&mut);
138 ctx = _ucma_find_context(id, file);
139 if (!IS_ERR(ctx)) {
140 if (ctx->closing)
141 ctx = ERR_PTR(-EIO);
142 else
143 atomic_inc(&ctx->ref);
144 }
145 mutex_unlock(&mut);
146 return ctx;
147 }
148
ucma_put_ctx(struct ucma_context * ctx)149 static void ucma_put_ctx(struct ucma_context *ctx)
150 {
151 if (atomic_dec_and_test(&ctx->ref))
152 complete(&ctx->comp);
153 }
154
155 /*
156 * Same as ucm_get_ctx but requires that ->cm_id->device is valid, eg that the
157 * CM_ID is bound.
158 */
ucma_get_ctx_dev(struct ucma_file * file,int id)159 static struct ucma_context *ucma_get_ctx_dev(struct ucma_file *file, int id)
160 {
161 struct ucma_context *ctx = ucma_get_ctx(file, id);
162
163 if (IS_ERR(ctx))
164 return ctx;
165 if (!ctx->cm_id->device) {
166 ucma_put_ctx(ctx);
167 return ERR_PTR(-EINVAL);
168 }
169 return ctx;
170 }
171
ucma_close_event_id(struct work_struct * work)172 static void ucma_close_event_id(struct work_struct *work)
173 {
174 struct ucma_event *uevent_close = container_of(work, struct ucma_event, close_work);
175
176 rdma_destroy_id(uevent_close->cm_id);
177 kfree(uevent_close);
178 }
179
ucma_close_id(struct work_struct * work)180 static void ucma_close_id(struct work_struct *work)
181 {
182 struct ucma_context *ctx = container_of(work, struct ucma_context, close_work);
183
184 /* once all inflight tasks are finished, we close all underlying
185 * resources. The context is still alive till its explicit destryoing
186 * by its creator.
187 */
188 ucma_put_ctx(ctx);
189 wait_for_completion(&ctx->comp);
190 /* No new events will be generated after destroying the id. */
191 rdma_destroy_id(ctx->cm_id);
192 }
193
ucma_alloc_ctx(struct ucma_file * file)194 static struct ucma_context *ucma_alloc_ctx(struct ucma_file *file)
195 {
196 struct ucma_context *ctx;
197
198 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
199 if (!ctx)
200 return NULL;
201
202 INIT_WORK(&ctx->close_work, ucma_close_id);
203 atomic_set(&ctx->ref, 1);
204 init_completion(&ctx->comp);
205 INIT_LIST_HEAD(&ctx->mc_list);
206 ctx->file = file;
207
208 mutex_lock(&mut);
209 ctx->id = idr_alloc(&ctx_idr, ctx, 0, 0, GFP_KERNEL);
210 mutex_unlock(&mut);
211 if (ctx->id < 0)
212 goto error;
213
214 list_add_tail(&ctx->list, &file->ctx_list);
215 return ctx;
216
217 error:
218 kfree(ctx);
219 return NULL;
220 }
221
ucma_alloc_multicast(struct ucma_context * ctx)222 static struct ucma_multicast* ucma_alloc_multicast(struct ucma_context *ctx)
223 {
224 struct ucma_multicast *mc;
225
226 mc = kzalloc(sizeof(*mc), GFP_KERNEL);
227 if (!mc)
228 return NULL;
229
230 mutex_lock(&mut);
231 mc->id = idr_alloc(&multicast_idr, mc, 0, 0, GFP_KERNEL);
232 mutex_unlock(&mut);
233 if (mc->id < 0)
234 goto error;
235
236 mc->ctx = ctx;
237 list_add_tail(&mc->list, &ctx->mc_list);
238 return mc;
239
240 error:
241 kfree(mc);
242 return NULL;
243 }
244
ucma_copy_conn_event(struct rdma_ucm_conn_param * dst,struct rdma_conn_param * src)245 static void ucma_copy_conn_event(struct rdma_ucm_conn_param *dst,
246 struct rdma_conn_param *src)
247 {
248 if (src->private_data_len)
249 memcpy(dst->private_data, src->private_data,
250 src->private_data_len);
251 dst->private_data_len = src->private_data_len;
252 dst->responder_resources =src->responder_resources;
253 dst->initiator_depth = src->initiator_depth;
254 dst->flow_control = src->flow_control;
255 dst->retry_count = src->retry_count;
256 dst->rnr_retry_count = src->rnr_retry_count;
257 dst->srq = src->srq;
258 dst->qp_num = src->qp_num;
259 }
260
ucma_copy_ud_event(struct ib_device * device,struct rdma_ucm_ud_param * dst,struct rdma_ud_param * src)261 static void ucma_copy_ud_event(struct ib_device *device,
262 struct rdma_ucm_ud_param *dst,
263 struct rdma_ud_param *src)
264 {
265 if (src->private_data_len)
266 memcpy(dst->private_data, src->private_data,
267 src->private_data_len);
268 dst->private_data_len = src->private_data_len;
269 ib_copy_ah_attr_to_user(device, &dst->ah_attr, &src->ah_attr);
270 dst->qp_num = src->qp_num;
271 dst->qkey = src->qkey;
272 }
273
ucma_set_event_context(struct ucma_context * ctx,struct rdma_cm_event * event,struct ucma_event * uevent)274 static void ucma_set_event_context(struct ucma_context *ctx,
275 struct rdma_cm_event *event,
276 struct ucma_event *uevent)
277 {
278 uevent->ctx = ctx;
279 switch (event->event) {
280 case RDMA_CM_EVENT_MULTICAST_JOIN:
281 case RDMA_CM_EVENT_MULTICAST_ERROR:
282 uevent->mc = __DECONST(struct ucma_multicast *,
283 event->param.ud.private_data);
284 uevent->resp.uid = uevent->mc->uid;
285 uevent->resp.id = uevent->mc->id;
286 break;
287 default:
288 uevent->resp.uid = ctx->uid;
289 uevent->resp.id = ctx->id;
290 break;
291 }
292 }
293
294 /* Called with file->mut locked for the relevant context. */
ucma_removal_event_handler(struct rdma_cm_id * cm_id)295 static void ucma_removal_event_handler(struct rdma_cm_id *cm_id)
296 {
297 struct ucma_context *ctx = cm_id->context;
298 struct ucma_event *con_req_eve;
299 int event_found = 0;
300
301 if (ctx->destroying)
302 return;
303
304 /* only if context is pointing to cm_id that it owns it and can be
305 * queued to be closed, otherwise that cm_id is an inflight one that
306 * is part of that context event list pending to be detached and
307 * reattached to its new context as part of ucma_get_event,
308 * handled separately below.
309 */
310 if (ctx->cm_id == cm_id) {
311 mutex_lock(&mut);
312 ctx->closing = 1;
313 mutex_unlock(&mut);
314 queue_work(ctx->file->close_wq, &ctx->close_work);
315 return;
316 }
317
318 list_for_each_entry(con_req_eve, &ctx->file->event_list, list) {
319 if (con_req_eve->cm_id == cm_id &&
320 con_req_eve->resp.event == RDMA_CM_EVENT_CONNECT_REQUEST) {
321 list_del(&con_req_eve->list);
322 INIT_WORK(&con_req_eve->close_work, ucma_close_event_id);
323 queue_work(ctx->file->close_wq, &con_req_eve->close_work);
324 event_found = 1;
325 break;
326 }
327 }
328 if (!event_found)
329 pr_err("ucma_removal_event_handler: warning: connect request event wasn't found\n");
330 }
331
ucma_event_handler(struct rdma_cm_id * cm_id,struct rdma_cm_event * event)332 static int ucma_event_handler(struct rdma_cm_id *cm_id,
333 struct rdma_cm_event *event)
334 {
335 struct ucma_event *uevent;
336 struct ucma_context *ctx = cm_id->context;
337 int ret = 0;
338
339 uevent = kzalloc(sizeof(*uevent), GFP_KERNEL);
340 if (!uevent)
341 return event->event == RDMA_CM_EVENT_CONNECT_REQUEST;
342
343 mutex_lock(&ctx->file->mut);
344 uevent->cm_id = cm_id;
345 ucma_set_event_context(ctx, event, uevent);
346 uevent->resp.event = event->event;
347 uevent->resp.status = event->status;
348 if (cm_id->qp_type == IB_QPT_UD)
349 ucma_copy_ud_event(cm_id->device, &uevent->resp.param.ud,
350 &event->param.ud);
351 else
352 ucma_copy_conn_event(&uevent->resp.param.conn,
353 &event->param.conn);
354
355 if (event->event == RDMA_CM_EVENT_CONNECT_REQUEST) {
356 if (!ctx->backlog) {
357 ret = -ENOMEM;
358 kfree(uevent);
359 goto out;
360 }
361 ctx->backlog--;
362 } else if (!ctx->uid || ctx->cm_id != cm_id) {
363 /*
364 * We ignore events for new connections until userspace has set
365 * their context. This can only happen if an error occurs on a
366 * new connection before the user accepts it. This is okay,
367 * since the accept will just fail later. However, we do need
368 * to release the underlying HW resources in case of a device
369 * removal event.
370 */
371 if (event->event == RDMA_CM_EVENT_DEVICE_REMOVAL)
372 ucma_removal_event_handler(cm_id);
373
374 kfree(uevent);
375 goto out;
376 }
377
378 list_add_tail(&uevent->list, &ctx->file->event_list);
379 wake_up_interruptible(&ctx->file->poll_wait);
380 if (event->event == RDMA_CM_EVENT_DEVICE_REMOVAL)
381 ucma_removal_event_handler(cm_id);
382 out:
383 mutex_unlock(&ctx->file->mut);
384 return ret;
385 }
386
ucma_get_event(struct ucma_file * file,const char __user * inbuf,int in_len,int out_len)387 static ssize_t ucma_get_event(struct ucma_file *file, const char __user *inbuf,
388 int in_len, int out_len)
389 {
390 struct ucma_context *ctx;
391 struct rdma_ucm_get_event cmd;
392 struct ucma_event *uevent;
393 int ret = 0;
394
395 if (out_len < sizeof uevent->resp)
396 return -ENOSPC;
397
398 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
399 return -EFAULT;
400
401 mutex_lock(&file->mut);
402 while (list_empty(&file->event_list)) {
403 mutex_unlock(&file->mut);
404
405 if (file->filp->f_flags & O_NONBLOCK)
406 return -EAGAIN;
407
408 if (wait_event_interruptible(file->poll_wait,
409 !list_empty(&file->event_list)))
410 return -ERESTARTSYS;
411
412 mutex_lock(&file->mut);
413 }
414
415 uevent = list_entry(file->event_list.next, struct ucma_event, list);
416
417 if (uevent->resp.event == RDMA_CM_EVENT_CONNECT_REQUEST) {
418 ctx = ucma_alloc_ctx(file);
419 if (!ctx) {
420 ret = -ENOMEM;
421 goto done;
422 }
423 uevent->ctx->backlog++;
424 ctx->cm_id = uevent->cm_id;
425 ctx->cm_id->context = ctx;
426 uevent->resp.id = ctx->id;
427 }
428
429 if (copy_to_user((void __user *)(unsigned long)cmd.response,
430 &uevent->resp, sizeof uevent->resp)) {
431 ret = -EFAULT;
432 goto done;
433 }
434
435 list_del(&uevent->list);
436 uevent->ctx->events_reported++;
437 if (uevent->mc)
438 uevent->mc->events_reported++;
439 kfree(uevent);
440 done:
441 mutex_unlock(&file->mut);
442 return ret;
443 }
444
ucma_get_qp_type(struct rdma_ucm_create_id * cmd,enum ib_qp_type * qp_type)445 static int ucma_get_qp_type(struct rdma_ucm_create_id *cmd, enum ib_qp_type *qp_type)
446 {
447 switch (cmd->ps) {
448 case RDMA_PS_TCP:
449 *qp_type = IB_QPT_RC;
450 return 0;
451 case RDMA_PS_UDP:
452 case RDMA_PS_IPOIB:
453 *qp_type = IB_QPT_UD;
454 return 0;
455 case RDMA_PS_IB:
456 *qp_type = cmd->qp_type;
457 return 0;
458 default:
459 return -EINVAL;
460 }
461 }
462
ucma_create_id(struct ucma_file * file,const char __user * inbuf,int in_len,int out_len)463 static ssize_t ucma_create_id(struct ucma_file *file, const char __user *inbuf,
464 int in_len, int out_len)
465 {
466 struct rdma_ucm_create_id cmd;
467 struct rdma_ucm_create_id_resp resp;
468 struct ucma_context *ctx;
469 struct rdma_cm_id *cm_id;
470 enum ib_qp_type qp_type;
471 int ret;
472
473 if (out_len < sizeof(resp))
474 return -ENOSPC;
475
476 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
477 return -EFAULT;
478
479 ret = ucma_get_qp_type(&cmd, &qp_type);
480 if (ret)
481 return ret;
482
483 mutex_lock(&file->mut);
484 ctx = ucma_alloc_ctx(file);
485 mutex_unlock(&file->mut);
486 if (!ctx)
487 return -ENOMEM;
488
489 ctx->uid = cmd.uid;
490 cm_id = rdma_create_id(TD_TO_VNET(curthread),
491 ucma_event_handler, ctx, cmd.ps, qp_type);
492 if (IS_ERR(cm_id)) {
493 ret = PTR_ERR(cm_id);
494 goto err1;
495 }
496
497 resp.id = ctx->id;
498 if (copy_to_user((void __user *)(unsigned long)cmd.response,
499 &resp, sizeof(resp))) {
500 ret = -EFAULT;
501 goto err2;
502 }
503
504 ctx->cm_id = cm_id;
505 return 0;
506
507 err2:
508 rdma_destroy_id(cm_id);
509 err1:
510 mutex_lock(&mut);
511 idr_remove(&ctx_idr, ctx->id);
512 mutex_unlock(&mut);
513 mutex_lock(&file->mut);
514 list_del(&ctx->list);
515 mutex_unlock(&file->mut);
516 kfree(ctx);
517 return ret;
518 }
519
ucma_cleanup_multicast(struct ucma_context * ctx)520 static void ucma_cleanup_multicast(struct ucma_context *ctx)
521 {
522 struct ucma_multicast *mc, *tmp;
523
524 mutex_lock(&mut);
525 list_for_each_entry_safe(mc, tmp, &ctx->mc_list, list) {
526 list_del(&mc->list);
527 idr_remove(&multicast_idr, mc->id);
528 kfree(mc);
529 }
530 mutex_unlock(&mut);
531 }
532
ucma_cleanup_mc_events(struct ucma_multicast * mc)533 static void ucma_cleanup_mc_events(struct ucma_multicast *mc)
534 {
535 struct ucma_event *uevent, *tmp;
536
537 list_for_each_entry_safe(uevent, tmp, &mc->ctx->file->event_list, list) {
538 if (uevent->mc != mc)
539 continue;
540
541 list_del(&uevent->list);
542 kfree(uevent);
543 }
544 }
545
546 /*
547 * ucma_free_ctx is called after the underlying rdma CM-ID is destroyed. At
548 * this point, no new events will be reported from the hardware. However, we
549 * still need to cleanup the UCMA context for this ID. Specifically, there
550 * might be events that have not yet been consumed by the user space software.
551 * These might include pending connect requests which we have not completed
552 * processing. We cannot call rdma_destroy_id while holding the lock of the
553 * context (file->mut), as it might cause a deadlock. We therefore extract all
554 * relevant events from the context pending events list while holding the
555 * mutex. After that we release them as needed.
556 */
ucma_free_ctx(struct ucma_context * ctx)557 static int ucma_free_ctx(struct ucma_context *ctx)
558 {
559 int events_reported;
560 struct ucma_event *uevent, *tmp;
561 LIST_HEAD(list);
562
563
564 ucma_cleanup_multicast(ctx);
565
566 /* Cleanup events not yet reported to the user. */
567 mutex_lock(&ctx->file->mut);
568 list_for_each_entry_safe(uevent, tmp, &ctx->file->event_list, list) {
569 if (uevent->ctx == ctx)
570 list_move_tail(&uevent->list, &list);
571 }
572 list_del(&ctx->list);
573 mutex_unlock(&ctx->file->mut);
574
575 list_for_each_entry_safe(uevent, tmp, &list, list) {
576 list_del(&uevent->list);
577 if (uevent->resp.event == RDMA_CM_EVENT_CONNECT_REQUEST)
578 rdma_destroy_id(uevent->cm_id);
579 kfree(uevent);
580 }
581
582 events_reported = ctx->events_reported;
583 kfree(ctx);
584 return events_reported;
585 }
586
ucma_destroy_id(struct ucma_file * file,const char __user * inbuf,int in_len,int out_len)587 static ssize_t ucma_destroy_id(struct ucma_file *file, const char __user *inbuf,
588 int in_len, int out_len)
589 {
590 struct rdma_ucm_destroy_id cmd;
591 struct rdma_ucm_destroy_id_resp resp;
592 struct ucma_context *ctx;
593 int ret = 0;
594
595 if (out_len < sizeof(resp))
596 return -ENOSPC;
597
598 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
599 return -EFAULT;
600
601 mutex_lock(&mut);
602 ctx = _ucma_find_context(cmd.id, file);
603 if (!IS_ERR(ctx))
604 idr_remove(&ctx_idr, ctx->id);
605 mutex_unlock(&mut);
606
607 if (IS_ERR(ctx))
608 return PTR_ERR(ctx);
609
610 mutex_lock(&ctx->file->mut);
611 ctx->destroying = 1;
612 mutex_unlock(&ctx->file->mut);
613
614 flush_workqueue(ctx->file->close_wq);
615 /* At this point it's guaranteed that there is no inflight
616 * closing task */
617 mutex_lock(&mut);
618 if (!ctx->closing) {
619 mutex_unlock(&mut);
620 ucma_put_ctx(ctx);
621 wait_for_completion(&ctx->comp);
622 rdma_destroy_id(ctx->cm_id);
623 } else {
624 mutex_unlock(&mut);
625 }
626
627 resp.events_reported = ucma_free_ctx(ctx);
628 if (copy_to_user((void __user *)(unsigned long)cmd.response,
629 &resp, sizeof(resp)))
630 ret = -EFAULT;
631
632 return ret;
633 }
634
ucma_bind_ip(struct ucma_file * file,const char __user * inbuf,int in_len,int out_len)635 static ssize_t ucma_bind_ip(struct ucma_file *file, const char __user *inbuf,
636 int in_len, int out_len)
637 {
638 struct rdma_ucm_bind_ip cmd;
639 struct ucma_context *ctx;
640 int ret;
641
642 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
643 return -EFAULT;
644
645 if (!rdma_addr_size_in6(&cmd.addr))
646 return -EINVAL;
647
648 ctx = ucma_get_ctx(file, cmd.id);
649 if (IS_ERR(ctx))
650 return PTR_ERR(ctx);
651
652 ret = rdma_bind_addr(ctx->cm_id, (struct sockaddr *) &cmd.addr);
653 ucma_put_ctx(ctx);
654 return ret;
655 }
656
ucma_bind(struct ucma_file * file,const char __user * inbuf,int in_len,int out_len)657 static ssize_t ucma_bind(struct ucma_file *file, const char __user *inbuf,
658 int in_len, int out_len)
659 {
660 struct rdma_ucm_bind cmd;
661 struct ucma_context *ctx;
662 int ret;
663
664 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
665 return -EFAULT;
666
667 if (cmd.reserved || !cmd.addr_size ||
668 cmd.addr_size != rdma_addr_size_kss(&cmd.addr))
669 return -EINVAL;
670
671 ctx = ucma_get_ctx(file, cmd.id);
672 if (IS_ERR(ctx))
673 return PTR_ERR(ctx);
674
675 ret = rdma_bind_addr(ctx->cm_id, (struct sockaddr *) &cmd.addr);
676 ucma_put_ctx(ctx);
677 return ret;
678 }
679
ucma_resolve_ip(struct ucma_file * file,const char __user * inbuf,int in_len,int out_len)680 static ssize_t ucma_resolve_ip(struct ucma_file *file,
681 const char __user *inbuf,
682 int in_len, int out_len)
683 {
684 struct rdma_ucm_resolve_ip cmd;
685 struct ucma_context *ctx;
686 int ret;
687
688 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
689 return -EFAULT;
690
691 if ((cmd.src_addr.sin6_family && !rdma_addr_size_in6(&cmd.src_addr)) ||
692 !rdma_addr_size_in6(&cmd.dst_addr))
693 return -EINVAL;
694
695 ctx = ucma_get_ctx(file, cmd.id);
696 if (IS_ERR(ctx))
697 return PTR_ERR(ctx);
698
699 ret = rdma_resolve_addr(ctx->cm_id, (struct sockaddr *) &cmd.src_addr,
700 (struct sockaddr *) &cmd.dst_addr, cmd.timeout_ms);
701 ucma_put_ctx(ctx);
702 return ret;
703 }
704
ucma_resolve_addr(struct ucma_file * file,const char __user * inbuf,int in_len,int out_len)705 static ssize_t ucma_resolve_addr(struct ucma_file *file,
706 const char __user *inbuf,
707 int in_len, int out_len)
708 {
709 struct rdma_ucm_resolve_addr cmd;
710 struct ucma_context *ctx;
711 int ret;
712
713 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
714 return -EFAULT;
715
716 if (cmd.reserved ||
717 (cmd.src_size && (cmd.src_size != rdma_addr_size_kss(&cmd.src_addr))) ||
718 !cmd.dst_size || (cmd.dst_size != rdma_addr_size_kss(&cmd.dst_addr)))
719 return -EINVAL;
720
721 ctx = ucma_get_ctx(file, cmd.id);
722 if (IS_ERR(ctx))
723 return PTR_ERR(ctx);
724
725 ret = rdma_resolve_addr(ctx->cm_id, (struct sockaddr *) &cmd.src_addr,
726 (struct sockaddr *) &cmd.dst_addr, cmd.timeout_ms);
727 ucma_put_ctx(ctx);
728 return ret;
729 }
730
ucma_resolve_route(struct ucma_file * file,const char __user * inbuf,int in_len,int out_len)731 static ssize_t ucma_resolve_route(struct ucma_file *file,
732 const char __user *inbuf,
733 int in_len, int out_len)
734 {
735 struct rdma_ucm_resolve_route cmd;
736 struct ucma_context *ctx;
737 int ret;
738
739 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
740 return -EFAULT;
741
742 ctx = ucma_get_ctx_dev(file, cmd.id);
743 if (IS_ERR(ctx))
744 return PTR_ERR(ctx);
745
746 ret = rdma_resolve_route(ctx->cm_id, cmd.timeout_ms);
747 ucma_put_ctx(ctx);
748 return ret;
749 }
750
ucma_copy_ib_route(struct rdma_ucm_query_route_resp * resp,struct rdma_route * route)751 static void ucma_copy_ib_route(struct rdma_ucm_query_route_resp *resp,
752 struct rdma_route *route)
753 {
754 struct rdma_dev_addr *dev_addr;
755
756 resp->num_paths = route->num_paths;
757 switch (route->num_paths) {
758 case 0:
759 dev_addr = &route->addr.dev_addr;
760 rdma_addr_get_dgid(dev_addr,
761 (union ib_gid *) &resp->ib_route[0].dgid);
762 rdma_addr_get_sgid(dev_addr,
763 (union ib_gid *) &resp->ib_route[0].sgid);
764 resp->ib_route[0].pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
765 break;
766 case 2:
767 ib_copy_path_rec_to_user(&resp->ib_route[1],
768 &route->path_rec[1]);
769 /* fall through */
770 case 1:
771 ib_copy_path_rec_to_user(&resp->ib_route[0],
772 &route->path_rec[0]);
773 break;
774 default:
775 break;
776 }
777 }
778
ucma_copy_iboe_route(struct rdma_ucm_query_route_resp * resp,struct rdma_route * route)779 static void ucma_copy_iboe_route(struct rdma_ucm_query_route_resp *resp,
780 struct rdma_route *route)
781 {
782
783 resp->num_paths = route->num_paths;
784 switch (route->num_paths) {
785 case 0:
786 rdma_ip2gid((struct sockaddr *)&route->addr.dst_addr,
787 (union ib_gid *)&resp->ib_route[0].dgid);
788 rdma_ip2gid((struct sockaddr *)&route->addr.src_addr,
789 (union ib_gid *)&resp->ib_route[0].sgid);
790 resp->ib_route[0].pkey = cpu_to_be16(0xffff);
791 break;
792 case 2:
793 ib_copy_path_rec_to_user(&resp->ib_route[1],
794 &route->path_rec[1]);
795 /* fall through */
796 case 1:
797 ib_copy_path_rec_to_user(&resp->ib_route[0],
798 &route->path_rec[0]);
799 break;
800 default:
801 break;
802 }
803 }
804
ucma_copy_iw_route(struct rdma_ucm_query_route_resp * resp,struct rdma_route * route)805 static void ucma_copy_iw_route(struct rdma_ucm_query_route_resp *resp,
806 struct rdma_route *route)
807 {
808 struct rdma_dev_addr *dev_addr;
809
810 dev_addr = &route->addr.dev_addr;
811 rdma_addr_get_dgid(dev_addr, (union ib_gid *) &resp->ib_route[0].dgid);
812 rdma_addr_get_sgid(dev_addr, (union ib_gid *) &resp->ib_route[0].sgid);
813 }
814
ucma_query_route(struct ucma_file * file,const char __user * inbuf,int in_len,int out_len)815 static ssize_t ucma_query_route(struct ucma_file *file,
816 const char __user *inbuf,
817 int in_len, int out_len)
818 {
819 struct rdma_ucm_query cmd;
820 struct rdma_ucm_query_route_resp resp;
821 struct ucma_context *ctx;
822 struct sockaddr *addr;
823 int ret = 0;
824
825 if (out_len < sizeof(resp))
826 return -ENOSPC;
827
828 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
829 return -EFAULT;
830
831 ctx = ucma_get_ctx(file, cmd.id);
832 if (IS_ERR(ctx))
833 return PTR_ERR(ctx);
834
835 memset(&resp, 0, sizeof resp);
836 addr = (struct sockaddr *) &ctx->cm_id->route.addr.src_addr;
837 memcpy(&resp.src_addr, addr, addr->sa_family == AF_INET ?
838 sizeof(struct sockaddr_in) :
839 sizeof(struct sockaddr_in6));
840 addr = (struct sockaddr *) &ctx->cm_id->route.addr.dst_addr;
841 memcpy(&resp.dst_addr, addr, addr->sa_family == AF_INET ?
842 sizeof(struct sockaddr_in) :
843 sizeof(struct sockaddr_in6));
844 if (!ctx->cm_id->device)
845 goto out;
846
847 resp.node_guid = (__force __u64) ctx->cm_id->device->node_guid;
848 resp.port_num = ctx->cm_id->port_num;
849
850 if (rdma_cap_ib_sa(ctx->cm_id->device, ctx->cm_id->port_num))
851 ucma_copy_ib_route(&resp, &ctx->cm_id->route);
852 else if (rdma_protocol_roce(ctx->cm_id->device, ctx->cm_id->port_num))
853 ucma_copy_iboe_route(&resp, &ctx->cm_id->route);
854 else if (rdma_protocol_iwarp(ctx->cm_id->device, ctx->cm_id->port_num))
855 ucma_copy_iw_route(&resp, &ctx->cm_id->route);
856
857 out:
858 if (copy_to_user((void __user *)(unsigned long)cmd.response,
859 &resp, sizeof(resp)))
860 ret = -EFAULT;
861
862 ucma_put_ctx(ctx);
863 return ret;
864 }
865
ucma_query_device_addr(struct rdma_cm_id * cm_id,struct rdma_ucm_query_addr_resp * resp)866 static void ucma_query_device_addr(struct rdma_cm_id *cm_id,
867 struct rdma_ucm_query_addr_resp *resp)
868 {
869 if (!cm_id->device)
870 return;
871
872 resp->node_guid = (__force __u64) cm_id->device->node_guid;
873 resp->port_num = cm_id->port_num;
874 resp->pkey = (__force __u16) cpu_to_be16(
875 ib_addr_get_pkey(&cm_id->route.addr.dev_addr));
876 }
877
ucma_query_addr(struct ucma_context * ctx,void __user * response,int out_len)878 static ssize_t ucma_query_addr(struct ucma_context *ctx,
879 void __user *response, int out_len)
880 {
881 struct rdma_ucm_query_addr_resp resp;
882 struct sockaddr *addr;
883 int ret = 0;
884
885 if (out_len < sizeof(resp))
886 return -ENOSPC;
887
888 memset(&resp, 0, sizeof resp);
889
890 addr = (struct sockaddr *) &ctx->cm_id->route.addr.src_addr;
891 resp.src_size = rdma_addr_size(addr);
892 memcpy(&resp.src_addr, addr, resp.src_size);
893
894 addr = (struct sockaddr *) &ctx->cm_id->route.addr.dst_addr;
895 resp.dst_size = rdma_addr_size(addr);
896 memcpy(&resp.dst_addr, addr, resp.dst_size);
897
898 ucma_query_device_addr(ctx->cm_id, &resp);
899
900 if (copy_to_user(response, &resp, sizeof(resp)))
901 ret = -EFAULT;
902
903 return ret;
904 }
905
ucma_query_path(struct ucma_context * ctx,void __user * response,int out_len)906 static ssize_t ucma_query_path(struct ucma_context *ctx,
907 void __user *response, int out_len)
908 {
909 struct rdma_ucm_query_path_resp *resp;
910 int i, ret = 0;
911
912 if (out_len < sizeof(*resp))
913 return -ENOSPC;
914
915 resp = kzalloc(out_len, GFP_KERNEL);
916 if (!resp)
917 return -ENOMEM;
918
919 resp->num_paths = ctx->cm_id->route.num_paths;
920 for (i = 0, out_len -= sizeof(*resp);
921 i < resp->num_paths && out_len > sizeof(struct ib_path_rec_data);
922 i++, out_len -= sizeof(struct ib_path_rec_data)) {
923 struct sa_path_rec *rec = &ctx->cm_id->route.path_rec[i];
924
925 resp->path_data[i].flags = IB_PATH_GMP | IB_PATH_PRIMARY |
926 IB_PATH_BIDIRECTIONAL;
927 if (rec->rec_type == SA_PATH_REC_TYPE_OPA) {
928 struct sa_path_rec ib;
929
930 sa_convert_path_opa_to_ib(&ib, rec);
931 ib_sa_pack_path(&ib, &resp->path_data[i].path_rec);
932
933 } else {
934 ib_sa_pack_path(rec, &resp->path_data[i].path_rec);
935 }
936 }
937
938 if (copy_to_user(response, resp,
939 sizeof(*resp) + (i * sizeof(struct ib_path_rec_data))))
940 ret = -EFAULT;
941
942 kfree(resp);
943 return ret;
944 }
945
ucma_query_gid(struct ucma_context * ctx,void __user * response,int out_len)946 static ssize_t ucma_query_gid(struct ucma_context *ctx,
947 void __user *response, int out_len)
948 {
949 struct rdma_ucm_query_addr_resp resp;
950 struct sockaddr_ib *addr;
951 int ret = 0;
952
953 if (out_len < sizeof(resp))
954 return -ENOSPC;
955
956 memset(&resp, 0, sizeof resp);
957
958 ucma_query_device_addr(ctx->cm_id, &resp);
959
960 addr = (struct sockaddr_ib *) &resp.src_addr;
961 resp.src_size = sizeof(*addr);
962 if (ctx->cm_id->route.addr.src_addr.ss_family == AF_IB) {
963 memcpy(addr, &ctx->cm_id->route.addr.src_addr, resp.src_size);
964 } else {
965 addr->sib_family = AF_IB;
966 addr->sib_pkey = (__force __be16) resp.pkey;
967 rdma_read_gids(ctx->cm_id, (union ib_gid *)&addr->sib_addr,
968 NULL);
969 addr->sib_sid = rdma_get_service_id(ctx->cm_id, (struct sockaddr *)
970 &ctx->cm_id->route.addr.src_addr);
971 }
972
973 addr = (struct sockaddr_ib *) &resp.dst_addr;
974 resp.dst_size = sizeof(*addr);
975 if (ctx->cm_id->route.addr.dst_addr.ss_family == AF_IB) {
976 memcpy(addr, &ctx->cm_id->route.addr.dst_addr, resp.dst_size);
977 } else {
978 addr->sib_family = AF_IB;
979 addr->sib_pkey = (__force __be16) resp.pkey;
980 rdma_read_gids(ctx->cm_id, NULL,
981 (union ib_gid *)&addr->sib_addr);
982 addr->sib_sid = rdma_get_service_id(ctx->cm_id, (struct sockaddr *)
983 &ctx->cm_id->route.addr.dst_addr);
984 }
985
986 if (copy_to_user(response, &resp, sizeof(resp)))
987 ret = -EFAULT;
988
989 return ret;
990 }
991
ucma_query(struct ucma_file * file,const char __user * inbuf,int in_len,int out_len)992 static ssize_t ucma_query(struct ucma_file *file,
993 const char __user *inbuf,
994 int in_len, int out_len)
995 {
996 struct rdma_ucm_query cmd;
997 struct ucma_context *ctx;
998 void __user *response;
999 int ret;
1000
1001 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
1002 return -EFAULT;
1003
1004 response = (void __user *)(unsigned long) cmd.response;
1005 ctx = ucma_get_ctx(file, cmd.id);
1006 if (IS_ERR(ctx))
1007 return PTR_ERR(ctx);
1008
1009 switch (cmd.option) {
1010 case RDMA_USER_CM_QUERY_ADDR:
1011 ret = ucma_query_addr(ctx, response, out_len);
1012 break;
1013 case RDMA_USER_CM_QUERY_PATH:
1014 ret = ucma_query_path(ctx, response, out_len);
1015 break;
1016 case RDMA_USER_CM_QUERY_GID:
1017 ret = ucma_query_gid(ctx, response, out_len);
1018 break;
1019 default:
1020 ret = -ENOSYS;
1021 break;
1022 }
1023
1024 ucma_put_ctx(ctx);
1025 return ret;
1026 }
1027
ucma_copy_conn_param(struct rdma_cm_id * id,struct rdma_conn_param * dst,struct rdma_ucm_conn_param * src)1028 static void ucma_copy_conn_param(struct rdma_cm_id *id,
1029 struct rdma_conn_param *dst,
1030 struct rdma_ucm_conn_param *src)
1031 {
1032 dst->private_data = src->private_data;
1033 dst->private_data_len = src->private_data_len;
1034 dst->responder_resources =src->responder_resources;
1035 dst->initiator_depth = src->initiator_depth;
1036 dst->flow_control = src->flow_control;
1037 dst->retry_count = src->retry_count;
1038 dst->rnr_retry_count = src->rnr_retry_count;
1039 dst->srq = src->srq;
1040 dst->qp_num = src->qp_num;
1041 dst->qkey = (id->route.addr.src_addr.ss_family == AF_IB) ? src->qkey : 0;
1042 }
1043
ucma_connect(struct ucma_file * file,const char __user * inbuf,int in_len,int out_len)1044 static ssize_t ucma_connect(struct ucma_file *file, const char __user *inbuf,
1045 int in_len, int out_len)
1046 {
1047 struct rdma_ucm_connect cmd;
1048 struct rdma_conn_param conn_param;
1049 struct ucma_context *ctx;
1050 int ret;
1051
1052 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
1053 return -EFAULT;
1054
1055 if (!cmd.conn_param.valid)
1056 return -EINVAL;
1057
1058 ctx = ucma_get_ctx_dev(file, cmd.id);
1059 if (IS_ERR(ctx))
1060 return PTR_ERR(ctx);
1061
1062 ucma_copy_conn_param(ctx->cm_id, &conn_param, &cmd.conn_param);
1063 ret = rdma_connect(ctx->cm_id, &conn_param);
1064 ucma_put_ctx(ctx);
1065 return ret;
1066 }
1067
ucma_listen(struct ucma_file * file,const char __user * inbuf,int in_len,int out_len)1068 static ssize_t ucma_listen(struct ucma_file *file, const char __user *inbuf,
1069 int in_len, int out_len)
1070 {
1071 struct rdma_ucm_listen cmd;
1072 struct ucma_context *ctx;
1073 int ret;
1074
1075 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
1076 return -EFAULT;
1077
1078 ctx = ucma_get_ctx(file, cmd.id);
1079 if (IS_ERR(ctx))
1080 return PTR_ERR(ctx);
1081
1082 ctx->backlog = cmd.backlog > 0 && cmd.backlog < max_backlog ?
1083 cmd.backlog : max_backlog;
1084 ret = rdma_listen(ctx->cm_id, ctx->backlog);
1085 ucma_put_ctx(ctx);
1086 return ret;
1087 }
1088
ucma_accept(struct ucma_file * file,const char __user * inbuf,int in_len,int out_len)1089 static ssize_t ucma_accept(struct ucma_file *file, const char __user *inbuf,
1090 int in_len, int out_len)
1091 {
1092 struct rdma_ucm_accept cmd;
1093 struct rdma_conn_param conn_param;
1094 struct ucma_context *ctx;
1095 int ret;
1096
1097 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
1098 return -EFAULT;
1099
1100 ctx = ucma_get_ctx_dev(file, cmd.id);
1101 if (IS_ERR(ctx))
1102 return PTR_ERR(ctx);
1103
1104 if (cmd.conn_param.valid) {
1105 ucma_copy_conn_param(ctx->cm_id, &conn_param, &cmd.conn_param);
1106 mutex_lock(&file->mut);
1107 ret = rdma_accept(ctx->cm_id, &conn_param);
1108 if (!ret)
1109 ctx->uid = cmd.uid;
1110 mutex_unlock(&file->mut);
1111 } else
1112 ret = rdma_accept(ctx->cm_id, NULL);
1113
1114 ucma_put_ctx(ctx);
1115 return ret;
1116 }
1117
ucma_reject(struct ucma_file * file,const char __user * inbuf,int in_len,int out_len)1118 static ssize_t ucma_reject(struct ucma_file *file, const char __user *inbuf,
1119 int in_len, int out_len)
1120 {
1121 struct rdma_ucm_reject cmd;
1122 struct ucma_context *ctx;
1123 int ret;
1124
1125 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
1126 return -EFAULT;
1127
1128 ctx = ucma_get_ctx_dev(file, cmd.id);
1129 if (IS_ERR(ctx))
1130 return PTR_ERR(ctx);
1131
1132 ret = rdma_reject(ctx->cm_id, cmd.private_data, cmd.private_data_len);
1133 ucma_put_ctx(ctx);
1134 return ret;
1135 }
1136
ucma_disconnect(struct ucma_file * file,const char __user * inbuf,int in_len,int out_len)1137 static ssize_t ucma_disconnect(struct ucma_file *file, const char __user *inbuf,
1138 int in_len, int out_len)
1139 {
1140 struct rdma_ucm_disconnect cmd;
1141 struct ucma_context *ctx;
1142 int ret;
1143
1144 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
1145 return -EFAULT;
1146
1147 ctx = ucma_get_ctx_dev(file, cmd.id);
1148 if (IS_ERR(ctx))
1149 return PTR_ERR(ctx);
1150
1151 ret = rdma_disconnect(ctx->cm_id);
1152 ucma_put_ctx(ctx);
1153 return ret;
1154 }
1155
ucma_init_qp_attr(struct ucma_file * file,const char __user * inbuf,int in_len,int out_len)1156 static ssize_t ucma_init_qp_attr(struct ucma_file *file,
1157 const char __user *inbuf,
1158 int in_len, int out_len)
1159 {
1160 struct rdma_ucm_init_qp_attr cmd;
1161 struct ib_uverbs_qp_attr resp;
1162 struct ucma_context *ctx;
1163 struct ib_qp_attr qp_attr;
1164 int ret;
1165
1166 if (out_len < sizeof(resp))
1167 return -ENOSPC;
1168
1169 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
1170 return -EFAULT;
1171
1172 ctx = ucma_get_ctx_dev(file, cmd.id);
1173 if (IS_ERR(ctx))
1174 return PTR_ERR(ctx);
1175
1176 resp.qp_attr_mask = 0;
1177 memset(&qp_attr, 0, sizeof qp_attr);
1178 qp_attr.qp_state = cmd.qp_state;
1179 ret = rdma_init_qp_attr(ctx->cm_id, &qp_attr, &resp.qp_attr_mask);
1180 if (ret)
1181 goto out;
1182
1183 ib_copy_qp_attr_to_user(ctx->cm_id->device, &resp, &qp_attr);
1184 if (copy_to_user((void __user *)(unsigned long)cmd.response,
1185 &resp, sizeof(resp)))
1186 ret = -EFAULT;
1187
1188 out:
1189 ucma_put_ctx(ctx);
1190 return ret;
1191 }
1192
ucma_set_option_id(struct ucma_context * ctx,int optname,void * optval,size_t optlen)1193 static int ucma_set_option_id(struct ucma_context *ctx, int optname,
1194 void *optval, size_t optlen)
1195 {
1196 int ret = 0;
1197
1198 switch (optname) {
1199 case RDMA_OPTION_ID_TOS:
1200 if (optlen != sizeof(u8)) {
1201 ret = -EINVAL;
1202 break;
1203 }
1204 rdma_set_service_type(ctx->cm_id, *((u8 *) optval));
1205 break;
1206 case RDMA_OPTION_ID_REUSEADDR:
1207 if (optlen != sizeof(int)) {
1208 ret = -EINVAL;
1209 break;
1210 }
1211 ret = rdma_set_reuseaddr(ctx->cm_id, *((int *) optval) ? 1 : 0);
1212 break;
1213 case RDMA_OPTION_ID_AFONLY:
1214 if (optlen != sizeof(int)) {
1215 ret = -EINVAL;
1216 break;
1217 }
1218 ret = rdma_set_afonly(ctx->cm_id, *((int *) optval) ? 1 : 0);
1219 break;
1220 case RDMA_OPTION_ID_ACK_TIMEOUT:
1221 if (optlen != sizeof(u8)) {
1222 ret = -EINVAL;
1223 break;
1224 }
1225 ret = rdma_set_ack_timeout(ctx->cm_id, *((u8 *)optval));
1226 break;
1227 default:
1228 ret = -ENOSYS;
1229 }
1230
1231 return ret;
1232 }
1233
ucma_set_ib_path(struct ucma_context * ctx,struct ib_path_rec_data * path_data,size_t optlen)1234 static int ucma_set_ib_path(struct ucma_context *ctx,
1235 struct ib_path_rec_data *path_data, size_t optlen)
1236 {
1237 struct sa_path_rec sa_path;
1238 struct rdma_cm_event event;
1239 int ret;
1240
1241 if (optlen % sizeof(*path_data))
1242 return -EINVAL;
1243
1244 for (; optlen; optlen -= sizeof(*path_data), path_data++) {
1245 if (path_data->flags == (IB_PATH_GMP | IB_PATH_PRIMARY |
1246 IB_PATH_BIDIRECTIONAL))
1247 break;
1248 }
1249
1250 if (!optlen)
1251 return -EINVAL;
1252
1253 if (!ctx->cm_id->device)
1254 return -EINVAL;
1255
1256 memset(&sa_path, 0, sizeof(sa_path));
1257
1258 sa_path.rec_type = SA_PATH_REC_TYPE_IB;
1259 ib_sa_unpack_path(path_data->path_rec, &sa_path);
1260
1261 if (rdma_cap_opa_ah(ctx->cm_id->device, ctx->cm_id->port_num)) {
1262 struct sa_path_rec opa;
1263
1264 sa_convert_path_ib_to_opa(&opa, &sa_path);
1265 ret = rdma_set_ib_path(ctx->cm_id, &opa);
1266 } else {
1267 ret = rdma_set_ib_path(ctx->cm_id, &sa_path);
1268 }
1269 if (ret)
1270 return ret;
1271
1272 memset(&event, 0, sizeof event);
1273 event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
1274 return ucma_event_handler(ctx->cm_id, &event);
1275 }
1276
ucma_set_option_ib(struct ucma_context * ctx,int optname,void * optval,size_t optlen)1277 static int ucma_set_option_ib(struct ucma_context *ctx, int optname,
1278 void *optval, size_t optlen)
1279 {
1280 int ret;
1281
1282 switch (optname) {
1283 case RDMA_OPTION_IB_PATH:
1284 ret = ucma_set_ib_path(ctx, optval, optlen);
1285 break;
1286 default:
1287 ret = -ENOSYS;
1288 }
1289
1290 return ret;
1291 }
1292
ucma_set_option_level(struct ucma_context * ctx,int level,int optname,void * optval,size_t optlen)1293 static int ucma_set_option_level(struct ucma_context *ctx, int level,
1294 int optname, void *optval, size_t optlen)
1295 {
1296 int ret;
1297
1298 switch (level) {
1299 case RDMA_OPTION_ID:
1300 ret = ucma_set_option_id(ctx, optname, optval, optlen);
1301 break;
1302 case RDMA_OPTION_IB:
1303 ret = ucma_set_option_ib(ctx, optname, optval, optlen);
1304 break;
1305 default:
1306 ret = -ENOSYS;
1307 }
1308
1309 return ret;
1310 }
1311
ucma_set_option(struct ucma_file * file,const char __user * inbuf,int in_len,int out_len)1312 static ssize_t ucma_set_option(struct ucma_file *file, const char __user *inbuf,
1313 int in_len, int out_len)
1314 {
1315 struct rdma_ucm_set_option cmd;
1316 struct ucma_context *ctx;
1317 void *optval;
1318 int ret;
1319
1320 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
1321 return -EFAULT;
1322
1323 ctx = ucma_get_ctx(file, cmd.id);
1324 if (IS_ERR(ctx))
1325 return PTR_ERR(ctx);
1326
1327 optval = memdup_user((void __user *) (unsigned long) cmd.optval,
1328 cmd.optlen);
1329 if (IS_ERR(optval)) {
1330 ret = PTR_ERR(optval);
1331 goto out;
1332 }
1333
1334 ret = ucma_set_option_level(ctx, cmd.level, cmd.optname, optval,
1335 cmd.optlen);
1336 kfree(optval);
1337
1338 out:
1339 ucma_put_ctx(ctx);
1340 return ret;
1341 }
1342
ucma_notify(struct ucma_file * file,const char __user * inbuf,int in_len,int out_len)1343 static ssize_t ucma_notify(struct ucma_file *file, const char __user *inbuf,
1344 int in_len, int out_len)
1345 {
1346 struct rdma_ucm_notify cmd;
1347 struct ucma_context *ctx;
1348 int ret;
1349
1350 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
1351 return -EFAULT;
1352
1353 ctx = ucma_get_ctx(file, cmd.id);
1354 if (IS_ERR(ctx))
1355 return PTR_ERR(ctx);
1356
1357 ret = rdma_notify(ctx->cm_id, (enum ib_event_type) cmd.event);
1358 ucma_put_ctx(ctx);
1359 return ret;
1360 }
1361
ucma_process_join(struct ucma_file * file,struct rdma_ucm_join_mcast * cmd,int out_len)1362 static ssize_t ucma_process_join(struct ucma_file *file,
1363 struct rdma_ucm_join_mcast *cmd, int out_len)
1364 {
1365 struct rdma_ucm_create_id_resp resp;
1366 struct ucma_context *ctx;
1367 struct ucma_multicast *mc;
1368 struct sockaddr *addr;
1369 int ret;
1370 u8 join_state;
1371
1372 if (out_len < sizeof(resp))
1373 return -ENOSPC;
1374
1375 addr = (struct sockaddr *) &cmd->addr;
1376 if (!cmd->addr_size || (cmd->addr_size != rdma_addr_size(addr)))
1377 return -EINVAL;
1378
1379 if (cmd->join_flags == RDMA_MC_JOIN_FLAG_FULLMEMBER)
1380 join_state = BIT(FULLMEMBER_JOIN);
1381 else if (cmd->join_flags == RDMA_MC_JOIN_FLAG_SENDONLY_FULLMEMBER)
1382 join_state = BIT(SENDONLY_FULLMEMBER_JOIN);
1383 else
1384 return -EINVAL;
1385
1386 ctx = ucma_get_ctx_dev(file, cmd->id);
1387 if (IS_ERR(ctx))
1388 return PTR_ERR(ctx);
1389
1390 mutex_lock(&file->mut);
1391 mc = ucma_alloc_multicast(ctx);
1392 if (!mc) {
1393 ret = -ENOMEM;
1394 goto err1;
1395 }
1396 mc->join_state = join_state;
1397 mc->uid = cmd->uid;
1398 memcpy(&mc->addr, addr, cmd->addr_size);
1399 ret = rdma_join_multicast(ctx->cm_id, (struct sockaddr *)&mc->addr,
1400 join_state, mc);
1401 if (ret)
1402 goto err2;
1403
1404 resp.id = mc->id;
1405 if (copy_to_user((void __user *)(unsigned long) cmd->response,
1406 &resp, sizeof(resp))) {
1407 ret = -EFAULT;
1408 goto err3;
1409 }
1410
1411 mutex_unlock(&file->mut);
1412 ucma_put_ctx(ctx);
1413 return 0;
1414
1415 err3:
1416 rdma_leave_multicast(ctx->cm_id, (struct sockaddr *) &mc->addr);
1417 ucma_cleanup_mc_events(mc);
1418 err2:
1419 mutex_lock(&mut);
1420 idr_remove(&multicast_idr, mc->id);
1421 mutex_unlock(&mut);
1422 list_del(&mc->list);
1423 kfree(mc);
1424 err1:
1425 mutex_unlock(&file->mut);
1426 ucma_put_ctx(ctx);
1427 return ret;
1428 }
1429
ucma_join_ip_multicast(struct ucma_file * file,const char __user * inbuf,int in_len,int out_len)1430 static ssize_t ucma_join_ip_multicast(struct ucma_file *file,
1431 const char __user *inbuf,
1432 int in_len, int out_len)
1433 {
1434 struct rdma_ucm_join_ip_mcast cmd;
1435 struct rdma_ucm_join_mcast join_cmd;
1436
1437 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
1438 return -EFAULT;
1439
1440 join_cmd.response = cmd.response;
1441 join_cmd.uid = cmd.uid;
1442 join_cmd.id = cmd.id;
1443 join_cmd.addr_size = rdma_addr_size_in6(&cmd.addr);
1444 join_cmd.join_flags = RDMA_MC_JOIN_FLAG_FULLMEMBER;
1445 memcpy(&join_cmd.addr, &cmd.addr, join_cmd.addr_size);
1446
1447 return ucma_process_join(file, &join_cmd, out_len);
1448 }
1449
ucma_join_multicast(struct ucma_file * file,const char __user * inbuf,int in_len,int out_len)1450 static ssize_t ucma_join_multicast(struct ucma_file *file,
1451 const char __user *inbuf,
1452 int in_len, int out_len)
1453 {
1454 struct rdma_ucm_join_mcast cmd;
1455
1456 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
1457 return -EFAULT;
1458
1459 if (!rdma_addr_size_kss(&cmd.addr))
1460 return -EINVAL;
1461
1462 return ucma_process_join(file, &cmd, out_len);
1463 }
1464
ucma_leave_multicast(struct ucma_file * file,const char __user * inbuf,int in_len,int out_len)1465 static ssize_t ucma_leave_multicast(struct ucma_file *file,
1466 const char __user *inbuf,
1467 int in_len, int out_len)
1468 {
1469 struct rdma_ucm_destroy_id cmd;
1470 struct rdma_ucm_destroy_id_resp resp;
1471 struct ucma_multicast *mc;
1472 int ret = 0;
1473
1474 if (out_len < sizeof(resp))
1475 return -ENOSPC;
1476
1477 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
1478 return -EFAULT;
1479
1480 mutex_lock(&mut);
1481 mc = idr_find(&multicast_idr, cmd.id);
1482 if (!mc)
1483 mc = ERR_PTR(-ENOENT);
1484 else if (mc->ctx->file != file)
1485 mc = ERR_PTR(-EINVAL);
1486 else if (!atomic_inc_not_zero(&mc->ctx->ref))
1487 mc = ERR_PTR(-ENXIO);
1488 else
1489 idr_remove(&multicast_idr, mc->id);
1490 mutex_unlock(&mut);
1491
1492 if (IS_ERR(mc)) {
1493 ret = PTR_ERR(mc);
1494 goto out;
1495 }
1496
1497 rdma_leave_multicast(mc->ctx->cm_id, (struct sockaddr *) &mc->addr);
1498 mutex_lock(&mc->ctx->file->mut);
1499 ucma_cleanup_mc_events(mc);
1500 list_del(&mc->list);
1501 mutex_unlock(&mc->ctx->file->mut);
1502
1503 ucma_put_ctx(mc->ctx);
1504 resp.events_reported = mc->events_reported;
1505 kfree(mc);
1506
1507 if (copy_to_user((void __user *)(unsigned long)cmd.response,
1508 &resp, sizeof(resp)))
1509 ret = -EFAULT;
1510 out:
1511 return ret;
1512 }
1513
ucma_lock_files(struct ucma_file * file1,struct ucma_file * file2)1514 static void ucma_lock_files(struct ucma_file *file1, struct ucma_file *file2)
1515 {
1516 /* Acquire mutex's based on pointer comparison to prevent deadlock. */
1517 if (file1 < file2) {
1518 mutex_lock(&file1->mut);
1519 mutex_lock_nested(&file2->mut, SINGLE_DEPTH_NESTING);
1520 } else {
1521 mutex_lock(&file2->mut);
1522 mutex_lock_nested(&file1->mut, SINGLE_DEPTH_NESTING);
1523 }
1524 }
1525
ucma_unlock_files(struct ucma_file * file1,struct ucma_file * file2)1526 static void ucma_unlock_files(struct ucma_file *file1, struct ucma_file *file2)
1527 {
1528 if (file1 < file2) {
1529 mutex_unlock(&file2->mut);
1530 mutex_unlock(&file1->mut);
1531 } else {
1532 mutex_unlock(&file1->mut);
1533 mutex_unlock(&file2->mut);
1534 }
1535 }
1536
ucma_move_events(struct ucma_context * ctx,struct ucma_file * file)1537 static void ucma_move_events(struct ucma_context *ctx, struct ucma_file *file)
1538 {
1539 struct ucma_event *uevent, *tmp;
1540
1541 list_for_each_entry_safe(uevent, tmp, &ctx->file->event_list, list)
1542 if (uevent->ctx == ctx)
1543 list_move_tail(&uevent->list, &file->event_list);
1544 }
1545
ucma_migrate_id(struct ucma_file * new_file,const char __user * inbuf,int in_len,int out_len)1546 static ssize_t ucma_migrate_id(struct ucma_file *new_file,
1547 const char __user *inbuf,
1548 int in_len, int out_len)
1549 {
1550 struct rdma_ucm_migrate_id cmd;
1551 struct rdma_ucm_migrate_resp resp;
1552 struct ucma_context *ctx;
1553 struct fd f;
1554 struct ucma_file *cur_file;
1555 int ret = 0;
1556
1557 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
1558 return -EFAULT;
1559
1560 /* Get current fd to protect against it being closed */
1561 f = fdget(cmd.fd);
1562 if (!f.file)
1563 return -ENOENT;
1564 if (f.file->f_op != &ucma_fops) {
1565 ret = -EINVAL;
1566 goto file_put;
1567 }
1568
1569 /* Validate current fd and prevent destruction of id. */
1570 ctx = ucma_get_ctx(f.file->private_data, cmd.id);
1571 if (IS_ERR(ctx)) {
1572 ret = PTR_ERR(ctx);
1573 goto file_put;
1574 }
1575
1576 cur_file = ctx->file;
1577 if (cur_file == new_file) {
1578 resp.events_reported = ctx->events_reported;
1579 goto response;
1580 }
1581
1582 /*
1583 * Migrate events between fd's, maintaining order, and avoiding new
1584 * events being added before existing events.
1585 */
1586 ucma_lock_files(cur_file, new_file);
1587 mutex_lock(&mut);
1588
1589 list_move_tail(&ctx->list, &new_file->ctx_list);
1590 ucma_move_events(ctx, new_file);
1591 ctx->file = new_file;
1592 resp.events_reported = ctx->events_reported;
1593
1594 mutex_unlock(&mut);
1595 ucma_unlock_files(cur_file, new_file);
1596
1597 response:
1598 if (copy_to_user((void __user *)(unsigned long)cmd.response,
1599 &resp, sizeof(resp)))
1600 ret = -EFAULT;
1601
1602 ucma_put_ctx(ctx);
1603 file_put:
1604 fdput(f);
1605 return ret;
1606 }
1607
1608 static ssize_t (*ucma_cmd_table[])(struct ucma_file *file,
1609 const char __user *inbuf,
1610 int in_len, int out_len) = {
1611 [RDMA_USER_CM_CMD_CREATE_ID] = ucma_create_id,
1612 [RDMA_USER_CM_CMD_DESTROY_ID] = ucma_destroy_id,
1613 [RDMA_USER_CM_CMD_BIND_IP] = ucma_bind_ip,
1614 [RDMA_USER_CM_CMD_RESOLVE_IP] = ucma_resolve_ip,
1615 [RDMA_USER_CM_CMD_RESOLVE_ROUTE] = ucma_resolve_route,
1616 [RDMA_USER_CM_CMD_QUERY_ROUTE] = ucma_query_route,
1617 [RDMA_USER_CM_CMD_CONNECT] = ucma_connect,
1618 [RDMA_USER_CM_CMD_LISTEN] = ucma_listen,
1619 [RDMA_USER_CM_CMD_ACCEPT] = ucma_accept,
1620 [RDMA_USER_CM_CMD_REJECT] = ucma_reject,
1621 [RDMA_USER_CM_CMD_DISCONNECT] = ucma_disconnect,
1622 [RDMA_USER_CM_CMD_INIT_QP_ATTR] = ucma_init_qp_attr,
1623 [RDMA_USER_CM_CMD_GET_EVENT] = ucma_get_event,
1624 [RDMA_USER_CM_CMD_GET_OPTION] = NULL,
1625 [RDMA_USER_CM_CMD_SET_OPTION] = ucma_set_option,
1626 [RDMA_USER_CM_CMD_NOTIFY] = ucma_notify,
1627 [RDMA_USER_CM_CMD_JOIN_IP_MCAST] = ucma_join_ip_multicast,
1628 [RDMA_USER_CM_CMD_LEAVE_MCAST] = ucma_leave_multicast,
1629 [RDMA_USER_CM_CMD_MIGRATE_ID] = ucma_migrate_id,
1630 [RDMA_USER_CM_CMD_QUERY] = ucma_query,
1631 [RDMA_USER_CM_CMD_BIND] = ucma_bind,
1632 [RDMA_USER_CM_CMD_RESOLVE_ADDR] = ucma_resolve_addr,
1633 [RDMA_USER_CM_CMD_JOIN_MCAST] = ucma_join_multicast
1634 };
1635
ucma_write(struct file * filp,const char __user * buf,size_t len,loff_t * pos)1636 static ssize_t ucma_write(struct file *filp, const char __user *buf,
1637 size_t len, loff_t *pos)
1638 {
1639 struct ucma_file *file = filp->private_data;
1640 struct rdma_ucm_cmd_hdr hdr;
1641 ssize_t ret;
1642
1643 if (!ib_safe_file_access(filp)) {
1644 pr_err_once("ucma_write: process %d (%s) changed security contexts after opening file descriptor, this is not allowed.\n",
1645 current->pid, current->comm);
1646 return -EACCES;
1647 }
1648
1649 if (len < sizeof(hdr))
1650 return -EINVAL;
1651
1652 if (copy_from_user(&hdr, buf, sizeof(hdr)))
1653 return -EFAULT;
1654
1655 if (hdr.cmd >= ARRAY_SIZE(ucma_cmd_table))
1656 return -EINVAL;
1657
1658 if (hdr.in + sizeof(hdr) > len)
1659 return -EINVAL;
1660
1661 if (!ucma_cmd_table[hdr.cmd])
1662 return -ENOSYS;
1663
1664 ret = ucma_cmd_table[hdr.cmd](file, buf + sizeof(hdr), hdr.in, hdr.out);
1665 if (!ret)
1666 ret = len;
1667
1668 return ret;
1669 }
1670
ucma_poll(struct file * filp,struct poll_table_struct * wait)1671 static unsigned int ucma_poll(struct file *filp, struct poll_table_struct *wait)
1672 {
1673 struct ucma_file *file = filp->private_data;
1674 unsigned int mask = 0;
1675
1676 poll_wait(filp, &file->poll_wait, wait);
1677
1678 if (!list_empty(&file->event_list))
1679 mask = POLLIN | POLLRDNORM;
1680
1681 return mask;
1682 }
1683
1684 /*
1685 * ucma_open() does not need the BKL:
1686 *
1687 * - no global state is referred to;
1688 * - there is no ioctl method to race against;
1689 * - no further module initialization is required for open to work
1690 * after the device is registered.
1691 */
ucma_open(struct inode * inode,struct file * filp)1692 static int ucma_open(struct inode *inode, struct file *filp)
1693 {
1694 struct ucma_file *file;
1695
1696 file = kmalloc(sizeof *file, GFP_KERNEL);
1697 if (!file)
1698 return -ENOMEM;
1699
1700 file->close_wq = alloc_ordered_workqueue("ucma_close_id",
1701 WQ_MEM_RECLAIM);
1702 if (!file->close_wq) {
1703 kfree(file);
1704 return -ENOMEM;
1705 }
1706
1707 INIT_LIST_HEAD(&file->event_list);
1708 INIT_LIST_HEAD(&file->ctx_list);
1709 init_waitqueue_head(&file->poll_wait);
1710 mutex_init(&file->mut);
1711
1712 filp->private_data = file;
1713 file->filp = filp;
1714
1715 return nonseekable_open(inode, filp);
1716 }
1717
ucma_close(struct inode * inode,struct file * filp)1718 static int ucma_close(struct inode *inode, struct file *filp)
1719 {
1720 struct ucma_file *file = filp->private_data;
1721 struct ucma_context *ctx, *tmp;
1722
1723 mutex_lock(&file->mut);
1724 list_for_each_entry_safe(ctx, tmp, &file->ctx_list, list) {
1725 ctx->destroying = 1;
1726 mutex_unlock(&file->mut);
1727
1728 mutex_lock(&mut);
1729 idr_remove(&ctx_idr, ctx->id);
1730 mutex_unlock(&mut);
1731
1732 flush_workqueue(file->close_wq);
1733 /* At that step once ctx was marked as destroying and workqueue
1734 * was flushed we are safe from any inflights handlers that
1735 * might put other closing task.
1736 */
1737 mutex_lock(&mut);
1738 if (!ctx->closing) {
1739 mutex_unlock(&mut);
1740 ucma_put_ctx(ctx);
1741 wait_for_completion(&ctx->comp);
1742 /* rdma_destroy_id ensures that no event handlers are
1743 * inflight for that id before releasing it.
1744 */
1745 rdma_destroy_id(ctx->cm_id);
1746 } else {
1747 mutex_unlock(&mut);
1748 }
1749
1750 ucma_free_ctx(ctx);
1751 mutex_lock(&file->mut);
1752 }
1753 mutex_unlock(&file->mut);
1754 destroy_workqueue(file->close_wq);
1755 kfree(file);
1756 return 0;
1757 }
1758
1759 static long
ucma_ioctl(struct file * filp,unsigned int cmd,unsigned long arg)1760 ucma_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1761 {
1762
1763 switch (cmd) {
1764 case FIONBIO:
1765 case FIOASYNC:
1766 return (0);
1767 default:
1768 return (-ENOTTY);
1769 }
1770 }
1771
1772 static const struct file_operations ucma_fops = {
1773 .owner = THIS_MODULE,
1774 .open = ucma_open,
1775 .release = ucma_close,
1776 .write = ucma_write,
1777 .unlocked_ioctl = ucma_ioctl,
1778 .poll = ucma_poll,
1779 .llseek = no_llseek,
1780 };
1781
1782 static struct miscdevice ucma_misc = {
1783 .minor = MISC_DYNAMIC_MINOR,
1784 .name = "rdma_cm",
1785 .nodename = "infiniband/rdma_cm",
1786 .mode = 0666,
1787 .fops = &ucma_fops,
1788 };
1789
show_abi_version(struct device * dev,struct device_attribute * attr,char * buf)1790 static ssize_t show_abi_version(struct device *dev,
1791 struct device_attribute *attr,
1792 char *buf)
1793 {
1794 return sprintf(buf, "%d\n", RDMA_USER_CM_ABI_VERSION);
1795 }
1796 static DEVICE_ATTR(abi_version, S_IRUGO, show_abi_version, NULL);
1797
ucma_init(void)1798 static int __init ucma_init(void)
1799 {
1800 int ret;
1801
1802 ret = misc_register(&ucma_misc);
1803 if (ret)
1804 return ret;
1805
1806 ret = device_create_file(ucma_misc.this_device, &dev_attr_abi_version);
1807 if (ret) {
1808 pr_err("rdma_ucm: couldn't create abi_version attr\n");
1809 goto err1;
1810 }
1811
1812 return 0;
1813 err1:
1814 misc_deregister(&ucma_misc);
1815 return ret;
1816 }
1817
ucma_cleanup(void)1818 static void __exit ucma_cleanup(void)
1819 {
1820 device_remove_file(ucma_misc.this_device, &dev_attr_abi_version);
1821 misc_deregister(&ucma_misc);
1822 idr_destroy(&ctx_idr);
1823 idr_destroy(&multicast_idr);
1824 }
1825
1826 module_init_order(ucma_init, SI_ORDER_FIFTH);
1827 module_exit_order(ucma_cleanup, SI_ORDER_FIFTH);
1828