2 * Copyright (c) 2005-2006 Intel Corporation. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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.
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
33 #include <linux/completion.h>
34 #include <linux/file.h>
35 #include <linux/mutex.h>
36 #include <linux/poll.h>
37 #include <linux/sched.h>
38 #include <linux/idr.h>
40 #include <linux/in6.h>
41 #include <linux/miscdevice.h>
42 #include <linux/slab.h>
43 #include <linux/sysctl.h>
44 #include <linux/module.h>
45 #include <linux/nsproxy.h>
47 #include <linux/nospec.h>
49 #include <rdma/rdma_user_cm.h>
50 #include <rdma/ib_marshall.h>
51 #include <rdma/rdma_cm.h>
52 #include <rdma/rdma_cm_ib.h>
53 #include <rdma/ib_addr.h>
56 MODULE_AUTHOR("Sean Hefty");
57 MODULE_DESCRIPTION("RDMA Userspace Connection Manager Access");
58 MODULE_LICENSE("Dual BSD/GPL");
60 static unsigned int max_backlog
= 1024;
62 static struct ctl_table_header
*ucma_ctl_table_hdr
;
63 static struct ctl_table ucma_ctl_table
[] = {
65 .procname
= "max_backlog",
67 .maxlen
= sizeof max_backlog
,
69 .proc_handler
= proc_dointvec
,
77 struct list_head ctx_list
;
78 struct list_head event_list
;
79 wait_queue_head_t poll_wait
;
80 struct workqueue_struct
*close_wq
;
85 struct completion comp
;
90 struct ucma_file
*file
;
91 struct rdma_cm_id
*cm_id
;
94 struct list_head list
;
95 struct list_head mc_list
;
96 /* mark that device is in process of destroying the internal HW
97 * resources, protected by the global mut
100 /* sync between removal event and id destroy, protected by file mut */
102 struct work_struct close_work
;
105 struct ucma_multicast
{
106 struct ucma_context
*ctx
;
112 struct list_head list
;
113 struct sockaddr_storage addr
;
117 struct ucma_context
*ctx
;
118 struct ucma_multicast
*mc
;
119 struct list_head list
;
120 struct rdma_cm_id
*cm_id
;
121 struct rdma_ucm_event_resp resp
;
122 struct work_struct close_work
;
125 static DEFINE_MUTEX(mut
);
126 static DEFINE_IDR(ctx_idr
);
127 static DEFINE_IDR(multicast_idr
);
129 static inline struct ucma_context
*_ucma_find_context(int id
,
130 struct ucma_file
*file
)
132 struct ucma_context
*ctx
;
134 ctx
= idr_find(&ctx_idr
, id
);
136 ctx
= ERR_PTR(-ENOENT
);
137 else if (ctx
->file
!= file
|| !ctx
->cm_id
)
138 ctx
= ERR_PTR(-EINVAL
);
142 static struct ucma_context
*ucma_get_ctx(struct ucma_file
*file
, int id
)
144 struct ucma_context
*ctx
;
147 ctx
= _ucma_find_context(id
, file
);
152 atomic_inc(&ctx
->ref
);
158 static void ucma_put_ctx(struct ucma_context
*ctx
)
160 if (atomic_dec_and_test(&ctx
->ref
))
161 complete(&ctx
->comp
);
164 static void ucma_close_event_id(struct work_struct
*work
)
166 struct ucma_event
*uevent_close
= container_of(work
, struct ucma_event
, close_work
);
168 rdma_destroy_id(uevent_close
->cm_id
);
172 static void ucma_close_id(struct work_struct
*work
)
174 struct ucma_context
*ctx
= container_of(work
, struct ucma_context
, close_work
);
176 /* once all inflight tasks are finished, we close all underlying
177 * resources. The context is still alive till its explicit destryoing
181 wait_for_completion(&ctx
->comp
);
182 /* No new events will be generated after destroying the id. */
183 rdma_destroy_id(ctx
->cm_id
);
186 static struct ucma_context
*ucma_alloc_ctx(struct ucma_file
*file
)
188 struct ucma_context
*ctx
;
190 ctx
= kzalloc(sizeof(*ctx
), GFP_KERNEL
);
194 INIT_WORK(&ctx
->close_work
, ucma_close_id
);
195 atomic_set(&ctx
->ref
, 1);
196 init_completion(&ctx
->comp
);
197 INIT_LIST_HEAD(&ctx
->mc_list
);
201 ctx
->id
= idr_alloc(&ctx_idr
, ctx
, 0, 0, GFP_KERNEL
);
206 list_add_tail(&ctx
->list
, &file
->ctx_list
);
214 static struct ucma_multicast
* ucma_alloc_multicast(struct ucma_context
*ctx
)
216 struct ucma_multicast
*mc
;
218 mc
= kzalloc(sizeof(*mc
), GFP_KERNEL
);
223 mc
->id
= idr_alloc(&multicast_idr
, NULL
, 0, 0, GFP_KERNEL
);
229 list_add_tail(&mc
->list
, &ctx
->mc_list
);
237 static void ucma_copy_conn_event(struct rdma_ucm_conn_param
*dst
,
238 struct rdma_conn_param
*src
)
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
;
250 dst
->qp_num
= src
->qp_num
;
253 static void ucma_copy_ud_event(struct ib_device
*device
,
254 struct rdma_ucm_ud_param
*dst
,
255 struct rdma_ud_param
*src
)
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
;
261 ib_copy_ah_attr_to_user(device
, &dst
->ah_attr
, &src
->ah_attr
);
262 dst
->qp_num
= src
->qp_num
;
263 dst
->qkey
= src
->qkey
;
266 static void ucma_set_event_context(struct ucma_context
*ctx
,
267 struct rdma_cm_event
*event
,
268 struct ucma_event
*uevent
)
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
;
280 uevent
->resp
.uid
= ctx
->uid
;
281 uevent
->resp
.id
= ctx
->id
;
286 /* Called with file->mut locked for the relevant context. */
287 static void ucma_removal_event_handler(struct rdma_cm_id
*cm_id
)
289 struct ucma_context
*ctx
= cm_id
->context
;
290 struct ucma_event
*con_req_eve
;
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.
302 if (ctx
->cm_id
== cm_id
) {
306 queue_work(ctx
->file
->close_wq
, &ctx
->close_work
);
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
);
321 pr_err("ucma_removal_event_handler: warning: connect request event wasn't found\n");
324 static int ucma_event_handler(struct rdma_cm_id
*cm_id
,
325 struct rdma_cm_event
*event
)
327 struct ucma_event
*uevent
;
328 struct ucma_context
*ctx
= cm_id
->context
;
331 uevent
= kzalloc(sizeof(*uevent
), GFP_KERNEL
);
333 return event
->event
== RDMA_CM_EVENT_CONNECT_REQUEST
;
335 mutex_lock(&ctx
->file
->mut
);
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
;
340 if (cm_id
->qp_type
== IB_QPT_UD
)
341 ucma_copy_ud_event(cm_id
->device
, &uevent
->resp
.param
.ud
,
344 ucma_copy_conn_event(&uevent
->resp
.param
.conn
,
347 if (event
->event
== RDMA_CM_EVENT_CONNECT_REQUEST
) {
354 } else if (!ctx
->uid
|| ctx
->cm_id
!= cm_id
) {
356 * We ignore events for new connections until userspace has set
357 * their context. This can only happen if an error occurs on a
358 * new connection before the user accepts it. This is okay,
359 * since the accept will just fail later. However, we do need
360 * to release the underlying HW resources in case of a device
363 if (event
->event
== RDMA_CM_EVENT_DEVICE_REMOVAL
)
364 ucma_removal_event_handler(cm_id
);
370 list_add_tail(&uevent
->list
, &ctx
->file
->event_list
);
371 wake_up_interruptible(&ctx
->file
->poll_wait
);
372 if (event
->event
== RDMA_CM_EVENT_DEVICE_REMOVAL
)
373 ucma_removal_event_handler(cm_id
);
375 mutex_unlock(&ctx
->file
->mut
);
379 static ssize_t
ucma_get_event(struct ucma_file
*file
, const char __user
*inbuf
,
380 int in_len
, int out_len
)
382 struct ucma_context
*ctx
;
383 struct rdma_ucm_get_event cmd
;
384 struct ucma_event
*uevent
;
387 if (out_len
< sizeof uevent
->resp
)
390 if (copy_from_user(&cmd
, inbuf
, sizeof(cmd
)))
393 mutex_lock(&file
->mut
);
394 while (list_empty(&file
->event_list
)) {
395 mutex_unlock(&file
->mut
);
397 if (file
->filp
->f_flags
& O_NONBLOCK
)
400 if (wait_event_interruptible(file
->poll_wait
,
401 !list_empty(&file
->event_list
)))
404 mutex_lock(&file
->mut
);
407 uevent
= list_entry(file
->event_list
.next
, struct ucma_event
, list
);
409 if (uevent
->resp
.event
== RDMA_CM_EVENT_CONNECT_REQUEST
) {
410 ctx
= ucma_alloc_ctx(file
);
415 uevent
->ctx
->backlog
++;
416 ctx
->cm_id
= uevent
->cm_id
;
417 ctx
->cm_id
->context
= ctx
;
418 uevent
->resp
.id
= ctx
->id
;
421 if (copy_to_user((void __user
*)(unsigned long)cmd
.response
,
422 &uevent
->resp
, sizeof uevent
->resp
)) {
427 list_del(&uevent
->list
);
428 uevent
->ctx
->events_reported
++;
430 uevent
->mc
->events_reported
++;
433 mutex_unlock(&file
->mut
);
437 static int ucma_get_qp_type(struct rdma_ucm_create_id
*cmd
, enum ib_qp_type
*qp_type
)
441 *qp_type
= IB_QPT_RC
;
445 *qp_type
= IB_QPT_UD
;
448 *qp_type
= cmd
->qp_type
;
455 static ssize_t
ucma_create_id(struct ucma_file
*file
, const char __user
*inbuf
,
456 int in_len
, int out_len
)
458 struct rdma_ucm_create_id cmd
;
459 struct rdma_ucm_create_id_resp resp
;
460 struct ucma_context
*ctx
;
461 struct rdma_cm_id
*cm_id
;
462 enum ib_qp_type qp_type
;
465 if (out_len
< sizeof(resp
))
468 if (copy_from_user(&cmd
, inbuf
, sizeof(cmd
)))
471 ret
= ucma_get_qp_type(&cmd
, &qp_type
);
475 mutex_lock(&file
->mut
);
476 ctx
= ucma_alloc_ctx(file
);
477 mutex_unlock(&file
->mut
);
482 cm_id
= rdma_create_id(current
->nsproxy
->net_ns
,
483 ucma_event_handler
, ctx
, cmd
.ps
, qp_type
);
485 ret
= PTR_ERR(cm_id
);
490 if (copy_to_user((void __user
*)(unsigned long)cmd
.response
,
491 &resp
, sizeof(resp
))) {
500 rdma_destroy_id(cm_id
);
503 idr_remove(&ctx_idr
, ctx
->id
);
505 mutex_lock(&file
->mut
);
506 list_del(&ctx
->list
);
507 mutex_unlock(&file
->mut
);
512 static void ucma_cleanup_multicast(struct ucma_context
*ctx
)
514 struct ucma_multicast
*mc
, *tmp
;
517 list_for_each_entry_safe(mc
, tmp
, &ctx
->mc_list
, list
) {
519 idr_remove(&multicast_idr
, mc
->id
);
525 static void ucma_cleanup_mc_events(struct ucma_multicast
*mc
)
527 struct ucma_event
*uevent
, *tmp
;
529 list_for_each_entry_safe(uevent
, tmp
, &mc
->ctx
->file
->event_list
, list
) {
530 if (uevent
->mc
!= mc
)
533 list_del(&uevent
->list
);
539 * ucma_free_ctx is called after the underlying rdma CM-ID is destroyed. At
540 * this point, no new events will be reported from the hardware. However, we
541 * still need to cleanup the UCMA context for this ID. Specifically, there
542 * might be events that have not yet been consumed by the user space software.
543 * These might include pending connect requests which we have not completed
544 * processing. We cannot call rdma_destroy_id while holding the lock of the
545 * context (file->mut), as it might cause a deadlock. We therefore extract all
546 * relevant events from the context pending events list while holding the
547 * mutex. After that we release them as needed.
549 static int ucma_free_ctx(struct ucma_context
*ctx
)
552 struct ucma_event
*uevent
, *tmp
;
556 ucma_cleanup_multicast(ctx
);
558 /* Cleanup events not yet reported to the user. */
559 mutex_lock(&ctx
->file
->mut
);
560 list_for_each_entry_safe(uevent
, tmp
, &ctx
->file
->event_list
, list
) {
561 if (uevent
->ctx
== ctx
)
562 list_move_tail(&uevent
->list
, &list
);
564 list_del(&ctx
->list
);
565 mutex_unlock(&ctx
->file
->mut
);
567 list_for_each_entry_safe(uevent
, tmp
, &list
, list
) {
568 list_del(&uevent
->list
);
569 if (uevent
->resp
.event
== RDMA_CM_EVENT_CONNECT_REQUEST
)
570 rdma_destroy_id(uevent
->cm_id
);
574 events_reported
= ctx
->events_reported
;
576 return events_reported
;
579 static ssize_t
ucma_destroy_id(struct ucma_file
*file
, const char __user
*inbuf
,
580 int in_len
, int out_len
)
582 struct rdma_ucm_destroy_id cmd
;
583 struct rdma_ucm_destroy_id_resp resp
;
584 struct ucma_context
*ctx
;
587 if (out_len
< sizeof(resp
))
590 if (copy_from_user(&cmd
, inbuf
, sizeof(cmd
)))
594 ctx
= _ucma_find_context(cmd
.id
, file
);
596 idr_remove(&ctx_idr
, ctx
->id
);
602 mutex_lock(&ctx
->file
->mut
);
604 mutex_unlock(&ctx
->file
->mut
);
606 flush_workqueue(ctx
->file
->close_wq
);
607 /* At this point it's guaranteed that there is no inflight
613 wait_for_completion(&ctx
->comp
);
614 rdma_destroy_id(ctx
->cm_id
);
619 resp
.events_reported
= ucma_free_ctx(ctx
);
620 if (copy_to_user((void __user
*)(unsigned long)cmd
.response
,
621 &resp
, sizeof(resp
)))
627 static ssize_t
ucma_bind_ip(struct ucma_file
*file
, const char __user
*inbuf
,
628 int in_len
, int out_len
)
630 struct rdma_ucm_bind_ip cmd
;
631 struct ucma_context
*ctx
;
634 if (copy_from_user(&cmd
, inbuf
, sizeof(cmd
)))
637 if (!rdma_addr_size_in6(&cmd
.addr
))
640 ctx
= ucma_get_ctx(file
, cmd
.id
);
644 ret
= rdma_bind_addr(ctx
->cm_id
, (struct sockaddr
*) &cmd
.addr
);
649 static ssize_t
ucma_bind(struct ucma_file
*file
, const char __user
*inbuf
,
650 int in_len
, int out_len
)
652 struct rdma_ucm_bind cmd
;
653 struct ucma_context
*ctx
;
656 if (copy_from_user(&cmd
, inbuf
, sizeof(cmd
)))
659 if (cmd
.reserved
|| !cmd
.addr_size
||
660 cmd
.addr_size
!= rdma_addr_size_kss(&cmd
.addr
))
663 ctx
= ucma_get_ctx(file
, cmd
.id
);
667 ret
= rdma_bind_addr(ctx
->cm_id
, (struct sockaddr
*) &cmd
.addr
);
672 static ssize_t
ucma_resolve_ip(struct ucma_file
*file
,
673 const char __user
*inbuf
,
674 int in_len
, int out_len
)
676 struct rdma_ucm_resolve_ip cmd
;
677 struct ucma_context
*ctx
;
680 if (copy_from_user(&cmd
, inbuf
, sizeof(cmd
)))
683 if ((cmd
.src_addr
.sin6_family
&& !rdma_addr_size_in6(&cmd
.src_addr
)) ||
684 !rdma_addr_size_in6(&cmd
.dst_addr
))
687 ctx
= ucma_get_ctx(file
, cmd
.id
);
691 ret
= rdma_resolve_addr(ctx
->cm_id
, (struct sockaddr
*) &cmd
.src_addr
,
692 (struct sockaddr
*) &cmd
.dst_addr
, cmd
.timeout_ms
);
697 static ssize_t
ucma_resolve_addr(struct ucma_file
*file
,
698 const char __user
*inbuf
,
699 int in_len
, int out_len
)
701 struct rdma_ucm_resolve_addr cmd
;
702 struct ucma_context
*ctx
;
705 if (copy_from_user(&cmd
, inbuf
, sizeof(cmd
)))
709 (cmd
.src_size
&& (cmd
.src_size
!= rdma_addr_size_kss(&cmd
.src_addr
))) ||
710 !cmd
.dst_size
|| (cmd
.dst_size
!= rdma_addr_size_kss(&cmd
.dst_addr
)))
713 ctx
= ucma_get_ctx(file
, cmd
.id
);
717 ret
= rdma_resolve_addr(ctx
->cm_id
, (struct sockaddr
*) &cmd
.src_addr
,
718 (struct sockaddr
*) &cmd
.dst_addr
, cmd
.timeout_ms
);
723 static ssize_t
ucma_resolve_route(struct ucma_file
*file
,
724 const char __user
*inbuf
,
725 int in_len
, int out_len
)
727 struct rdma_ucm_resolve_route cmd
;
728 struct ucma_context
*ctx
;
731 if (copy_from_user(&cmd
, inbuf
, sizeof(cmd
)))
734 ctx
= ucma_get_ctx(file
, cmd
.id
);
738 ret
= rdma_resolve_route(ctx
->cm_id
, cmd
.timeout_ms
);
743 static void ucma_copy_ib_route(struct rdma_ucm_query_route_resp
*resp
,
744 struct rdma_route
*route
)
746 struct rdma_dev_addr
*dev_addr
;
748 resp
->num_paths
= route
->num_paths
;
749 switch (route
->num_paths
) {
751 dev_addr
= &route
->addr
.dev_addr
;
752 rdma_addr_get_dgid(dev_addr
,
753 (union ib_gid
*) &resp
->ib_route
[0].dgid
);
754 rdma_addr_get_sgid(dev_addr
,
755 (union ib_gid
*) &resp
->ib_route
[0].sgid
);
756 resp
->ib_route
[0].pkey
= cpu_to_be16(ib_addr_get_pkey(dev_addr
));
759 ib_copy_path_rec_to_user(&resp
->ib_route
[1],
760 &route
->path_rec
[1]);
763 ib_copy_path_rec_to_user(&resp
->ib_route
[0],
764 &route
->path_rec
[0]);
771 static void ucma_copy_iboe_route(struct rdma_ucm_query_route_resp
*resp
,
772 struct rdma_route
*route
)
775 resp
->num_paths
= route
->num_paths
;
776 switch (route
->num_paths
) {
778 rdma_ip2gid((struct sockaddr
*)&route
->addr
.dst_addr
,
779 (union ib_gid
*)&resp
->ib_route
[0].dgid
);
780 rdma_ip2gid((struct sockaddr
*)&route
->addr
.src_addr
,
781 (union ib_gid
*)&resp
->ib_route
[0].sgid
);
782 resp
->ib_route
[0].pkey
= cpu_to_be16(0xffff);
785 ib_copy_path_rec_to_user(&resp
->ib_route
[1],
786 &route
->path_rec
[1]);
789 ib_copy_path_rec_to_user(&resp
->ib_route
[0],
790 &route
->path_rec
[0]);
797 static void ucma_copy_iw_route(struct rdma_ucm_query_route_resp
*resp
,
798 struct rdma_route
*route
)
800 struct rdma_dev_addr
*dev_addr
;
802 dev_addr
= &route
->addr
.dev_addr
;
803 rdma_addr_get_dgid(dev_addr
, (union ib_gid
*) &resp
->ib_route
[0].dgid
);
804 rdma_addr_get_sgid(dev_addr
, (union ib_gid
*) &resp
->ib_route
[0].sgid
);
807 static ssize_t
ucma_query_route(struct ucma_file
*file
,
808 const char __user
*inbuf
,
809 int in_len
, int out_len
)
811 struct rdma_ucm_query cmd
;
812 struct rdma_ucm_query_route_resp resp
;
813 struct ucma_context
*ctx
;
814 struct sockaddr
*addr
;
817 if (out_len
< sizeof(resp
))
820 if (copy_from_user(&cmd
, inbuf
, sizeof(cmd
)))
823 ctx
= ucma_get_ctx(file
, cmd
.id
);
827 memset(&resp
, 0, sizeof resp
);
828 addr
= (struct sockaddr
*) &ctx
->cm_id
->route
.addr
.src_addr
;
829 memcpy(&resp
.src_addr
, addr
, addr
->sa_family
== AF_INET
?
830 sizeof(struct sockaddr_in
) :
831 sizeof(struct sockaddr_in6
));
832 addr
= (struct sockaddr
*) &ctx
->cm_id
->route
.addr
.dst_addr
;
833 memcpy(&resp
.dst_addr
, addr
, addr
->sa_family
== AF_INET
?
834 sizeof(struct sockaddr_in
) :
835 sizeof(struct sockaddr_in6
));
836 if (!ctx
->cm_id
->device
)
839 resp
.node_guid
= (__force __u64
) ctx
->cm_id
->device
->node_guid
;
840 resp
.port_num
= ctx
->cm_id
->port_num
;
842 if (rdma_cap_ib_sa(ctx
->cm_id
->device
, ctx
->cm_id
->port_num
))
843 ucma_copy_ib_route(&resp
, &ctx
->cm_id
->route
);
844 else if (rdma_protocol_roce(ctx
->cm_id
->device
, ctx
->cm_id
->port_num
))
845 ucma_copy_iboe_route(&resp
, &ctx
->cm_id
->route
);
846 else if (rdma_protocol_iwarp(ctx
->cm_id
->device
, ctx
->cm_id
->port_num
))
847 ucma_copy_iw_route(&resp
, &ctx
->cm_id
->route
);
850 if (copy_to_user((void __user
*)(unsigned long)cmd
.response
,
851 &resp
, sizeof(resp
)))
858 static void ucma_query_device_addr(struct rdma_cm_id
*cm_id
,
859 struct rdma_ucm_query_addr_resp
*resp
)
864 resp
->node_guid
= (__force __u64
) cm_id
->device
->node_guid
;
865 resp
->port_num
= cm_id
->port_num
;
866 resp
->pkey
= (__force __u16
) cpu_to_be16(
867 ib_addr_get_pkey(&cm_id
->route
.addr
.dev_addr
));
870 static ssize_t
ucma_query_addr(struct ucma_context
*ctx
,
871 void __user
*response
, int out_len
)
873 struct rdma_ucm_query_addr_resp resp
;
874 struct sockaddr
*addr
;
877 if (out_len
< sizeof(resp
))
880 memset(&resp
, 0, sizeof resp
);
882 addr
= (struct sockaddr
*) &ctx
->cm_id
->route
.addr
.src_addr
;
883 resp
.src_size
= rdma_addr_size(addr
);
884 memcpy(&resp
.src_addr
, addr
, resp
.src_size
);
886 addr
= (struct sockaddr
*) &ctx
->cm_id
->route
.addr
.dst_addr
;
887 resp
.dst_size
= rdma_addr_size(addr
);
888 memcpy(&resp
.dst_addr
, addr
, resp
.dst_size
);
890 ucma_query_device_addr(ctx
->cm_id
, &resp
);
892 if (copy_to_user(response
, &resp
, sizeof(resp
)))
898 static ssize_t
ucma_query_path(struct ucma_context
*ctx
,
899 void __user
*response
, int out_len
)
901 struct rdma_ucm_query_path_resp
*resp
;
904 if (out_len
< sizeof(*resp
))
907 resp
= kzalloc(out_len
, GFP_KERNEL
);
911 resp
->num_paths
= ctx
->cm_id
->route
.num_paths
;
912 for (i
= 0, out_len
-= sizeof(*resp
);
913 i
< resp
->num_paths
&& out_len
> sizeof(struct ib_path_rec_data
);
914 i
++, out_len
-= sizeof(struct ib_path_rec_data
)) {
915 struct sa_path_rec
*rec
= &ctx
->cm_id
->route
.path_rec
[i
];
917 resp
->path_data
[i
].flags
= IB_PATH_GMP
| IB_PATH_PRIMARY
|
918 IB_PATH_BIDIRECTIONAL
;
919 if (rec
->rec_type
== SA_PATH_REC_TYPE_OPA
) {
920 struct sa_path_rec ib
;
922 sa_convert_path_opa_to_ib(&ib
, rec
);
923 ib_sa_pack_path(&ib
, &resp
->path_data
[i
].path_rec
);
926 ib_sa_pack_path(rec
, &resp
->path_data
[i
].path_rec
);
930 if (copy_to_user(response
, resp
,
931 sizeof(*resp
) + (i
* sizeof(struct ib_path_rec_data
))))
938 static ssize_t
ucma_query_gid(struct ucma_context
*ctx
,
939 void __user
*response
, int out_len
)
941 struct rdma_ucm_query_addr_resp resp
;
942 struct sockaddr_ib
*addr
;
945 if (out_len
< sizeof(resp
))
948 memset(&resp
, 0, sizeof resp
);
950 ucma_query_device_addr(ctx
->cm_id
, &resp
);
952 addr
= (struct sockaddr_ib
*) &resp
.src_addr
;
953 resp
.src_size
= sizeof(*addr
);
954 if (ctx
->cm_id
->route
.addr
.src_addr
.ss_family
== AF_IB
) {
955 memcpy(addr
, &ctx
->cm_id
->route
.addr
.src_addr
, resp
.src_size
);
957 addr
->sib_family
= AF_IB
;
958 addr
->sib_pkey
= (__force __be16
) resp
.pkey
;
959 rdma_addr_get_sgid(&ctx
->cm_id
->route
.addr
.dev_addr
,
960 (union ib_gid
*) &addr
->sib_addr
);
961 addr
->sib_sid
= rdma_get_service_id(ctx
->cm_id
, (struct sockaddr
*)
962 &ctx
->cm_id
->route
.addr
.src_addr
);
965 addr
= (struct sockaddr_ib
*) &resp
.dst_addr
;
966 resp
.dst_size
= sizeof(*addr
);
967 if (ctx
->cm_id
->route
.addr
.dst_addr
.ss_family
== AF_IB
) {
968 memcpy(addr
, &ctx
->cm_id
->route
.addr
.dst_addr
, resp
.dst_size
);
970 addr
->sib_family
= AF_IB
;
971 addr
->sib_pkey
= (__force __be16
) resp
.pkey
;
972 rdma_addr_get_dgid(&ctx
->cm_id
->route
.addr
.dev_addr
,
973 (union ib_gid
*) &addr
->sib_addr
);
974 addr
->sib_sid
= rdma_get_service_id(ctx
->cm_id
, (struct sockaddr
*)
975 &ctx
->cm_id
->route
.addr
.dst_addr
);
978 if (copy_to_user(response
, &resp
, sizeof(resp
)))
984 static ssize_t
ucma_query(struct ucma_file
*file
,
985 const char __user
*inbuf
,
986 int in_len
, int out_len
)
988 struct rdma_ucm_query cmd
;
989 struct ucma_context
*ctx
;
990 void __user
*response
;
993 if (copy_from_user(&cmd
, inbuf
, sizeof(cmd
)))
996 response
= (void __user
*)(unsigned long) cmd
.response
;
997 ctx
= ucma_get_ctx(file
, cmd
.id
);
1001 switch (cmd
.option
) {
1002 case RDMA_USER_CM_QUERY_ADDR
:
1003 ret
= ucma_query_addr(ctx
, response
, out_len
);
1005 case RDMA_USER_CM_QUERY_PATH
:
1006 ret
= ucma_query_path(ctx
, response
, out_len
);
1008 case RDMA_USER_CM_QUERY_GID
:
1009 ret
= ucma_query_gid(ctx
, response
, out_len
);
1020 static void ucma_copy_conn_param(struct rdma_cm_id
*id
,
1021 struct rdma_conn_param
*dst
,
1022 struct rdma_ucm_conn_param
*src
)
1024 dst
->private_data
= src
->private_data
;
1025 dst
->private_data_len
= src
->private_data_len
;
1026 dst
->responder_resources
=src
->responder_resources
;
1027 dst
->initiator_depth
= src
->initiator_depth
;
1028 dst
->flow_control
= src
->flow_control
;
1029 dst
->retry_count
= src
->retry_count
;
1030 dst
->rnr_retry_count
= src
->rnr_retry_count
;
1031 dst
->srq
= src
->srq
;
1032 dst
->qp_num
= src
->qp_num
;
1033 dst
->qkey
= (id
->route
.addr
.src_addr
.ss_family
== AF_IB
) ? src
->qkey
: 0;
1036 static ssize_t
ucma_connect(struct ucma_file
*file
, const char __user
*inbuf
,
1037 int in_len
, int out_len
)
1039 struct rdma_ucm_connect cmd
;
1040 struct rdma_conn_param conn_param
;
1041 struct ucma_context
*ctx
;
1044 if (copy_from_user(&cmd
, inbuf
, sizeof(cmd
)))
1047 if (!cmd
.conn_param
.valid
)
1050 ctx
= ucma_get_ctx(file
, cmd
.id
);
1052 return PTR_ERR(ctx
);
1054 ucma_copy_conn_param(ctx
->cm_id
, &conn_param
, &cmd
.conn_param
);
1055 ret
= rdma_connect(ctx
->cm_id
, &conn_param
);
1060 static ssize_t
ucma_listen(struct ucma_file
*file
, const char __user
*inbuf
,
1061 int in_len
, int out_len
)
1063 struct rdma_ucm_listen cmd
;
1064 struct ucma_context
*ctx
;
1067 if (copy_from_user(&cmd
, inbuf
, sizeof(cmd
)))
1070 ctx
= ucma_get_ctx(file
, cmd
.id
);
1072 return PTR_ERR(ctx
);
1074 ctx
->backlog
= cmd
.backlog
> 0 && cmd
.backlog
< max_backlog
?
1075 cmd
.backlog
: max_backlog
;
1076 ret
= rdma_listen(ctx
->cm_id
, ctx
->backlog
);
1081 static ssize_t
ucma_accept(struct ucma_file
*file
, const char __user
*inbuf
,
1082 int in_len
, int out_len
)
1084 struct rdma_ucm_accept cmd
;
1085 struct rdma_conn_param conn_param
;
1086 struct ucma_context
*ctx
;
1089 if (copy_from_user(&cmd
, inbuf
, sizeof(cmd
)))
1092 ctx
= ucma_get_ctx(file
, cmd
.id
);
1094 return PTR_ERR(ctx
);
1096 if (cmd
.conn_param
.valid
) {
1097 ucma_copy_conn_param(ctx
->cm_id
, &conn_param
, &cmd
.conn_param
);
1098 mutex_lock(&file
->mut
);
1099 ret
= rdma_accept(ctx
->cm_id
, &conn_param
);
1102 mutex_unlock(&file
->mut
);
1104 ret
= rdma_accept(ctx
->cm_id
, NULL
);
1110 static ssize_t
ucma_reject(struct ucma_file
*file
, const char __user
*inbuf
,
1111 int in_len
, int out_len
)
1113 struct rdma_ucm_reject cmd
;
1114 struct ucma_context
*ctx
;
1117 if (copy_from_user(&cmd
, inbuf
, sizeof(cmd
)))
1120 ctx
= ucma_get_ctx(file
, cmd
.id
);
1122 return PTR_ERR(ctx
);
1124 ret
= rdma_reject(ctx
->cm_id
, cmd
.private_data
, cmd
.private_data_len
);
1129 static ssize_t
ucma_disconnect(struct ucma_file
*file
, const char __user
*inbuf
,
1130 int in_len
, int out_len
)
1132 struct rdma_ucm_disconnect cmd
;
1133 struct ucma_context
*ctx
;
1136 if (copy_from_user(&cmd
, inbuf
, sizeof(cmd
)))
1139 ctx
= ucma_get_ctx(file
, cmd
.id
);
1141 return PTR_ERR(ctx
);
1143 ret
= rdma_disconnect(ctx
->cm_id
);
1148 static ssize_t
ucma_init_qp_attr(struct ucma_file
*file
,
1149 const char __user
*inbuf
,
1150 int in_len
, int out_len
)
1152 struct rdma_ucm_init_qp_attr cmd
;
1153 struct ib_uverbs_qp_attr resp
;
1154 struct ucma_context
*ctx
;
1155 struct ib_qp_attr qp_attr
;
1158 if (out_len
< sizeof(resp
))
1161 if (copy_from_user(&cmd
, inbuf
, sizeof(cmd
)))
1164 if (cmd
.qp_state
> IB_QPS_ERR
)
1167 ctx
= ucma_get_ctx(file
, cmd
.id
);
1169 return PTR_ERR(ctx
);
1171 if (!ctx
->cm_id
->device
) {
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
);
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
)))
1193 static int ucma_set_option_id(struct ucma_context
*ctx
, int optname
,
1194 void *optval
, size_t optlen
)
1199 case RDMA_OPTION_ID_TOS
:
1200 if (optlen
!= sizeof(u8
)) {
1204 rdma_set_service_type(ctx
->cm_id
, *((u8
*) optval
));
1206 case RDMA_OPTION_ID_REUSEADDR
:
1207 if (optlen
!= sizeof(int)) {
1211 ret
= rdma_set_reuseaddr(ctx
->cm_id
, *((int *) optval
) ? 1 : 0);
1213 case RDMA_OPTION_ID_AFONLY
:
1214 if (optlen
!= sizeof(int)) {
1218 ret
= rdma_set_afonly(ctx
->cm_id
, *((int *) optval
) ? 1 : 0);
1227 static int ucma_set_ib_path(struct ucma_context
*ctx
,
1228 struct ib_path_rec_data
*path_data
, size_t optlen
)
1230 struct sa_path_rec sa_path
;
1231 struct rdma_cm_event event
;
1234 if (optlen
% sizeof(*path_data
))
1237 for (; optlen
; optlen
-= sizeof(*path_data
), path_data
++) {
1238 if (path_data
->flags
== (IB_PATH_GMP
| IB_PATH_PRIMARY
|
1239 IB_PATH_BIDIRECTIONAL
))
1246 if (!ctx
->cm_id
->device
)
1249 memset(&sa_path
, 0, sizeof(sa_path
));
1251 sa_path
.rec_type
= SA_PATH_REC_TYPE_IB
;
1252 ib_sa_unpack_path(path_data
->path_rec
, &sa_path
);
1254 if (rdma_cap_opa_ah(ctx
->cm_id
->device
, ctx
->cm_id
->port_num
)) {
1255 struct sa_path_rec opa
;
1257 sa_convert_path_ib_to_opa(&opa
, &sa_path
);
1258 ret
= rdma_set_ib_paths(ctx
->cm_id
, &opa
, 1);
1260 ret
= rdma_set_ib_paths(ctx
->cm_id
, &sa_path
, 1);
1265 memset(&event
, 0, sizeof event
);
1266 event
.event
= RDMA_CM_EVENT_ROUTE_RESOLVED
;
1267 return ucma_event_handler(ctx
->cm_id
, &event
);
1270 static int ucma_set_option_ib(struct ucma_context
*ctx
, int optname
,
1271 void *optval
, size_t optlen
)
1276 case RDMA_OPTION_IB_PATH
:
1277 ret
= ucma_set_ib_path(ctx
, optval
, optlen
);
1286 static int ucma_set_option_level(struct ucma_context
*ctx
, int level
,
1287 int optname
, void *optval
, size_t optlen
)
1292 case RDMA_OPTION_ID
:
1293 ret
= ucma_set_option_id(ctx
, optname
, optval
, optlen
);
1295 case RDMA_OPTION_IB
:
1296 ret
= ucma_set_option_ib(ctx
, optname
, optval
, optlen
);
1305 static ssize_t
ucma_set_option(struct ucma_file
*file
, const char __user
*inbuf
,
1306 int in_len
, int out_len
)
1308 struct rdma_ucm_set_option cmd
;
1309 struct ucma_context
*ctx
;
1313 if (copy_from_user(&cmd
, inbuf
, sizeof(cmd
)))
1316 ctx
= ucma_get_ctx(file
, cmd
.id
);
1318 return PTR_ERR(ctx
);
1320 if (unlikely(cmd
.optlen
> KMALLOC_MAX_SIZE
))
1323 optval
= memdup_user((void __user
*) (unsigned long) cmd
.optval
,
1325 if (IS_ERR(optval
)) {
1326 ret
= PTR_ERR(optval
);
1330 ret
= ucma_set_option_level(ctx
, cmd
.level
, cmd
.optname
, optval
,
1339 static ssize_t
ucma_notify(struct ucma_file
*file
, const char __user
*inbuf
,
1340 int in_len
, int out_len
)
1342 struct rdma_ucm_notify cmd
;
1343 struct ucma_context
*ctx
;
1346 if (copy_from_user(&cmd
, inbuf
, sizeof(cmd
)))
1349 ctx
= ucma_get_ctx(file
, cmd
.id
);
1351 return PTR_ERR(ctx
);
1353 if (ctx
->cm_id
->device
)
1354 ret
= rdma_notify(ctx
->cm_id
, (enum ib_event_type
)cmd
.event
);
1360 static ssize_t
ucma_process_join(struct ucma_file
*file
,
1361 struct rdma_ucm_join_mcast
*cmd
, int out_len
)
1363 struct rdma_ucm_create_id_resp resp
;
1364 struct ucma_context
*ctx
;
1365 struct ucma_multicast
*mc
;
1366 struct sockaddr
*addr
;
1370 if (out_len
< sizeof(resp
))
1373 addr
= (struct sockaddr
*) &cmd
->addr
;
1374 if (cmd
->addr_size
!= rdma_addr_size(addr
))
1377 if (cmd
->join_flags
== RDMA_MC_JOIN_FLAG_FULLMEMBER
)
1378 join_state
= BIT(FULLMEMBER_JOIN
);
1379 else if (cmd
->join_flags
== RDMA_MC_JOIN_FLAG_SENDONLY_FULLMEMBER
)
1380 join_state
= BIT(SENDONLY_FULLMEMBER_JOIN
);
1384 ctx
= ucma_get_ctx(file
, cmd
->id
);
1386 return PTR_ERR(ctx
);
1388 mutex_lock(&file
->mut
);
1389 mc
= ucma_alloc_multicast(ctx
);
1394 mc
->join_state
= join_state
;
1396 memcpy(&mc
->addr
, addr
, cmd
->addr_size
);
1397 ret
= rdma_join_multicast(ctx
->cm_id
, (struct sockaddr
*)&mc
->addr
,
1403 if (copy_to_user((void __user
*)(unsigned long) cmd
->response
,
1404 &resp
, sizeof(resp
))) {
1410 idr_replace(&multicast_idr
, mc
, mc
->id
);
1413 mutex_unlock(&file
->mut
);
1418 rdma_leave_multicast(ctx
->cm_id
, (struct sockaddr
*) &mc
->addr
);
1419 ucma_cleanup_mc_events(mc
);
1422 idr_remove(&multicast_idr
, mc
->id
);
1424 list_del(&mc
->list
);
1427 mutex_unlock(&file
->mut
);
1432 static ssize_t
ucma_join_ip_multicast(struct ucma_file
*file
,
1433 const char __user
*inbuf
,
1434 int in_len
, int out_len
)
1436 struct rdma_ucm_join_ip_mcast cmd
;
1437 struct rdma_ucm_join_mcast join_cmd
;
1439 if (copy_from_user(&cmd
, inbuf
, sizeof(cmd
)))
1442 join_cmd
.response
= cmd
.response
;
1443 join_cmd
.uid
= cmd
.uid
;
1444 join_cmd
.id
= cmd
.id
;
1445 join_cmd
.addr_size
= rdma_addr_size_in6(&cmd
.addr
);
1446 if (!join_cmd
.addr_size
)
1449 join_cmd
.join_flags
= RDMA_MC_JOIN_FLAG_FULLMEMBER
;
1450 memcpy(&join_cmd
.addr
, &cmd
.addr
, join_cmd
.addr_size
);
1452 return ucma_process_join(file
, &join_cmd
, out_len
);
1455 static ssize_t
ucma_join_multicast(struct ucma_file
*file
,
1456 const char __user
*inbuf
,
1457 int in_len
, int out_len
)
1459 struct rdma_ucm_join_mcast cmd
;
1461 if (copy_from_user(&cmd
, inbuf
, sizeof(cmd
)))
1464 if (!rdma_addr_size_kss(&cmd
.addr
))
1467 return ucma_process_join(file
, &cmd
, out_len
);
1470 static ssize_t
ucma_leave_multicast(struct ucma_file
*file
,
1471 const char __user
*inbuf
,
1472 int in_len
, int out_len
)
1474 struct rdma_ucm_destroy_id cmd
;
1475 struct rdma_ucm_destroy_id_resp resp
;
1476 struct ucma_multicast
*mc
;
1479 if (out_len
< sizeof(resp
))
1482 if (copy_from_user(&cmd
, inbuf
, sizeof(cmd
)))
1486 mc
= idr_find(&multicast_idr
, cmd
.id
);
1488 mc
= ERR_PTR(-ENOENT
);
1489 else if (mc
->ctx
->file
!= file
)
1490 mc
= ERR_PTR(-EINVAL
);
1491 else if (!atomic_inc_not_zero(&mc
->ctx
->ref
))
1492 mc
= ERR_PTR(-ENXIO
);
1494 idr_remove(&multicast_idr
, mc
->id
);
1502 rdma_leave_multicast(mc
->ctx
->cm_id
, (struct sockaddr
*) &mc
->addr
);
1503 mutex_lock(&mc
->ctx
->file
->mut
);
1504 ucma_cleanup_mc_events(mc
);
1505 list_del(&mc
->list
);
1506 mutex_unlock(&mc
->ctx
->file
->mut
);
1508 ucma_put_ctx(mc
->ctx
);
1509 resp
.events_reported
= mc
->events_reported
;
1512 if (copy_to_user((void __user
*)(unsigned long)cmd
.response
,
1513 &resp
, sizeof(resp
)))
1519 static void ucma_lock_files(struct ucma_file
*file1
, struct ucma_file
*file2
)
1521 /* Acquire mutex's based on pointer comparison to prevent deadlock. */
1522 if (file1
< file2
) {
1523 mutex_lock(&file1
->mut
);
1524 mutex_lock_nested(&file2
->mut
, SINGLE_DEPTH_NESTING
);
1526 mutex_lock(&file2
->mut
);
1527 mutex_lock_nested(&file1
->mut
, SINGLE_DEPTH_NESTING
);
1531 static void ucma_unlock_files(struct ucma_file
*file1
, struct ucma_file
*file2
)
1533 if (file1
< file2
) {
1534 mutex_unlock(&file2
->mut
);
1535 mutex_unlock(&file1
->mut
);
1537 mutex_unlock(&file1
->mut
);
1538 mutex_unlock(&file2
->mut
);
1542 static void ucma_move_events(struct ucma_context
*ctx
, struct ucma_file
*file
)
1544 struct ucma_event
*uevent
, *tmp
;
1546 list_for_each_entry_safe(uevent
, tmp
, &ctx
->file
->event_list
, list
)
1547 if (uevent
->ctx
== ctx
)
1548 list_move_tail(&uevent
->list
, &file
->event_list
);
1551 static ssize_t
ucma_migrate_id(struct ucma_file
*new_file
,
1552 const char __user
*inbuf
,
1553 int in_len
, int out_len
)
1555 struct rdma_ucm_migrate_id cmd
;
1556 struct rdma_ucm_migrate_resp resp
;
1557 struct ucma_context
*ctx
;
1559 struct ucma_file
*cur_file
;
1562 if (copy_from_user(&cmd
, inbuf
, sizeof(cmd
)))
1565 /* Get current fd to protect against it being closed */
1570 /* Validate current fd and prevent destruction of id. */
1571 ctx
= ucma_get_ctx(f
.file
->private_data
, cmd
.id
);
1577 cur_file
= ctx
->file
;
1578 if (cur_file
== new_file
) {
1579 resp
.events_reported
= ctx
->events_reported
;
1584 * Migrate events between fd's, maintaining order, and avoiding new
1585 * events being added before existing events.
1587 ucma_lock_files(cur_file
, new_file
);
1590 list_move_tail(&ctx
->list
, &new_file
->ctx_list
);
1591 ucma_move_events(ctx
, new_file
);
1592 ctx
->file
= new_file
;
1593 resp
.events_reported
= ctx
->events_reported
;
1596 ucma_unlock_files(cur_file
, new_file
);
1599 if (copy_to_user((void __user
*)(unsigned long)cmd
.response
,
1600 &resp
, sizeof(resp
)))
1609 static ssize_t (*ucma_cmd_table
[])(struct ucma_file
*file
,
1610 const char __user
*inbuf
,
1611 int in_len
, int out_len
) = {
1612 [RDMA_USER_CM_CMD_CREATE_ID
] = ucma_create_id
,
1613 [RDMA_USER_CM_CMD_DESTROY_ID
] = ucma_destroy_id
,
1614 [RDMA_USER_CM_CMD_BIND_IP
] = ucma_bind_ip
,
1615 [RDMA_USER_CM_CMD_RESOLVE_IP
] = ucma_resolve_ip
,
1616 [RDMA_USER_CM_CMD_RESOLVE_ROUTE
] = ucma_resolve_route
,
1617 [RDMA_USER_CM_CMD_QUERY_ROUTE
] = ucma_query_route
,
1618 [RDMA_USER_CM_CMD_CONNECT
] = ucma_connect
,
1619 [RDMA_USER_CM_CMD_LISTEN
] = ucma_listen
,
1620 [RDMA_USER_CM_CMD_ACCEPT
] = ucma_accept
,
1621 [RDMA_USER_CM_CMD_REJECT
] = ucma_reject
,
1622 [RDMA_USER_CM_CMD_DISCONNECT
] = ucma_disconnect
,
1623 [RDMA_USER_CM_CMD_INIT_QP_ATTR
] = ucma_init_qp_attr
,
1624 [RDMA_USER_CM_CMD_GET_EVENT
] = ucma_get_event
,
1625 [RDMA_USER_CM_CMD_GET_OPTION
] = NULL
,
1626 [RDMA_USER_CM_CMD_SET_OPTION
] = ucma_set_option
,
1627 [RDMA_USER_CM_CMD_NOTIFY
] = ucma_notify
,
1628 [RDMA_USER_CM_CMD_JOIN_IP_MCAST
] = ucma_join_ip_multicast
,
1629 [RDMA_USER_CM_CMD_LEAVE_MCAST
] = ucma_leave_multicast
,
1630 [RDMA_USER_CM_CMD_MIGRATE_ID
] = ucma_migrate_id
,
1631 [RDMA_USER_CM_CMD_QUERY
] = ucma_query
,
1632 [RDMA_USER_CM_CMD_BIND
] = ucma_bind
,
1633 [RDMA_USER_CM_CMD_RESOLVE_ADDR
] = ucma_resolve_addr
,
1634 [RDMA_USER_CM_CMD_JOIN_MCAST
] = ucma_join_multicast
1637 static ssize_t
ucma_write(struct file
*filp
, const char __user
*buf
,
1638 size_t len
, loff_t
*pos
)
1640 struct ucma_file
*file
= filp
->private_data
;
1641 struct rdma_ucm_cmd_hdr hdr
;
1644 if (!ib_safe_file_access(filp
)) {
1645 pr_err_once("ucma_write: process %d (%s) changed security contexts after opening file descriptor, this is not allowed.\n",
1646 task_tgid_vnr(current
), current
->comm
);
1650 if (len
< sizeof(hdr
))
1653 if (copy_from_user(&hdr
, buf
, sizeof(hdr
)))
1656 if (hdr
.cmd
>= ARRAY_SIZE(ucma_cmd_table
))
1658 hdr
.cmd
= array_index_nospec(hdr
.cmd
, ARRAY_SIZE(ucma_cmd_table
));
1660 if (hdr
.in
+ sizeof(hdr
) > len
)
1663 if (!ucma_cmd_table
[hdr
.cmd
])
1666 ret
= ucma_cmd_table
[hdr
.cmd
](file
, buf
+ sizeof(hdr
), hdr
.in
, hdr
.out
);
1673 static unsigned int ucma_poll(struct file
*filp
, struct poll_table_struct
*wait
)
1675 struct ucma_file
*file
= filp
->private_data
;
1676 unsigned int mask
= 0;
1678 poll_wait(filp
, &file
->poll_wait
, wait
);
1680 if (!list_empty(&file
->event_list
))
1681 mask
= POLLIN
| POLLRDNORM
;
1687 * ucma_open() does not need the BKL:
1689 * - no global state is referred to;
1690 * - there is no ioctl method to race against;
1691 * - no further module initialization is required for open to work
1692 * after the device is registered.
1694 static int ucma_open(struct inode
*inode
, struct file
*filp
)
1696 struct ucma_file
*file
;
1698 file
= kmalloc(sizeof *file
, GFP_KERNEL
);
1702 file
->close_wq
= alloc_ordered_workqueue("ucma_close_id",
1704 if (!file
->close_wq
) {
1709 INIT_LIST_HEAD(&file
->event_list
);
1710 INIT_LIST_HEAD(&file
->ctx_list
);
1711 init_waitqueue_head(&file
->poll_wait
);
1712 mutex_init(&file
->mut
);
1714 filp
->private_data
= file
;
1717 return nonseekable_open(inode
, filp
);
1720 static int ucma_close(struct inode
*inode
, struct file
*filp
)
1722 struct ucma_file
*file
= filp
->private_data
;
1723 struct ucma_context
*ctx
, *tmp
;
1725 mutex_lock(&file
->mut
);
1726 list_for_each_entry_safe(ctx
, tmp
, &file
->ctx_list
, list
) {
1727 ctx
->destroying
= 1;
1728 mutex_unlock(&file
->mut
);
1731 idr_remove(&ctx_idr
, ctx
->id
);
1734 flush_workqueue(file
->close_wq
);
1735 /* At that step once ctx was marked as destroying and workqueue
1736 * was flushed we are safe from any inflights handlers that
1737 * might put other closing task.
1740 if (!ctx
->closing
) {
1742 /* rdma_destroy_id ensures that no event handlers are
1743 * inflight for that id before releasing it.
1745 rdma_destroy_id(ctx
->cm_id
);
1751 mutex_lock(&file
->mut
);
1753 mutex_unlock(&file
->mut
);
1754 destroy_workqueue(file
->close_wq
);
1759 static const struct file_operations ucma_fops
= {
1760 .owner
= THIS_MODULE
,
1762 .release
= ucma_close
,
1763 .write
= ucma_write
,
1765 .llseek
= no_llseek
,
1768 static struct miscdevice ucma_misc
= {
1769 .minor
= MISC_DYNAMIC_MINOR
,
1771 .nodename
= "infiniband/rdma_cm",
1776 static ssize_t
show_abi_version(struct device
*dev
,
1777 struct device_attribute
*attr
,
1780 return sprintf(buf
, "%d\n", RDMA_USER_CM_ABI_VERSION
);
1782 static DEVICE_ATTR(abi_version
, S_IRUGO
, show_abi_version
, NULL
);
1784 static int __init
ucma_init(void)
1788 ret
= misc_register(&ucma_misc
);
1792 ret
= device_create_file(ucma_misc
.this_device
, &dev_attr_abi_version
);
1794 pr_err("rdma_ucm: couldn't create abi_version attr\n");
1798 ucma_ctl_table_hdr
= register_net_sysctl(&init_net
, "net/rdma_ucm", ucma_ctl_table
);
1799 if (!ucma_ctl_table_hdr
) {
1800 pr_err("rdma_ucm: couldn't register sysctl paths\n");
1806 device_remove_file(ucma_misc
.this_device
, &dev_attr_abi_version
);
1808 misc_deregister(&ucma_misc
);
1812 static void __exit
ucma_cleanup(void)
1814 unregister_net_sysctl_table(ucma_ctl_table_hdr
);
1815 device_remove_file(ucma_misc
.this_device
, &dev_attr_abi_version
);
1816 misc_deregister(&ucma_misc
);
1817 idr_destroy(&ctx_idr
);
1818 idr_destroy(&multicast_idr
);
1821 module_init(ucma_init
);
1822 module_exit(ucma_cleanup
);