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