Skip to content

Commit 071ba4c

Browse files
committed
RDMA: Add rdma_connect_locked()
There are two flows for handling RDMA_CM_EVENT_ROUTE_RESOLVED, either the handler triggers a completion and another thread does rdma_connect() or the handler directly calls rdma_connect(). In all cases rdma_connect() needs to hold the handler_mutex, but when handler's are invoked this is already held by the core code. This causes ULPs using the 2nd method to deadlock. Provide a rdma_connect_locked() and have all ULPs call it from their handlers. Link: https://lore.kernel.org/r/[email protected] Reported-and-tested-by: Guoqing Jiang <[email protected]> Fixes: 2a7cec5 ("RDMA/cma: Fix locking for the RDMA_CM_CONNECT state") Acked-by: Santosh Shilimkar <[email protected]> Acked-by: Jack Wang <[email protected]> Reviewed-by: Christoph Hellwig <[email protected]> Reviewed-by: Max Gurtovoy <[email protected]> Reviewed-by: Sagi Grimberg <[email protected]> Signed-off-by: Jason Gunthorpe <[email protected]>
1 parent 7d66a71 commit 071ba4c

File tree

6 files changed

+48
-29
lines changed

6 files changed

+48
-29
lines changed

drivers/infiniband/core/cma.c

+38-10
Original file line numberDiff line numberDiff line change
@@ -405,10 +405,10 @@ static int cma_comp_exch(struct rdma_id_private *id_priv,
405405
/*
406406
* The FSM uses a funny double locking where state is protected by both
407407
* the handler_mutex and the spinlock. State is not allowed to change
408-
* away from a handler_mutex protected value without also holding
408+
* to/from a handler_mutex protected value without also holding
409409
* handler_mutex.
410410
*/
411-
if (comp == RDMA_CM_CONNECT)
411+
if (comp == RDMA_CM_CONNECT || exch == RDMA_CM_CONNECT)
412412
lockdep_assert_held(&id_priv->handler_mutex);
413413

414414
spin_lock_irqsave(&id_priv->lock, flags);
@@ -4038,17 +4038,23 @@ static int cma_connect_iw(struct rdma_id_private *id_priv,
40384038
return ret;
40394039
}
40404040

4041-
int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
4041+
/**
4042+
* rdma_connect_locked - Initiate an active connection request.
4043+
* @id: Connection identifier to connect.
4044+
* @conn_param: Connection information used for connected QPs.
4045+
*
4046+
* Same as rdma_connect() but can only be called from the
4047+
* RDMA_CM_EVENT_ROUTE_RESOLVED handler callback.
4048+
*/
4049+
int rdma_connect_locked(struct rdma_cm_id *id,
4050+
struct rdma_conn_param *conn_param)
40424051
{
40434052
struct rdma_id_private *id_priv =
40444053
container_of(id, struct rdma_id_private, id);
40454054
int ret;
40464055

4047-
mutex_lock(&id_priv->handler_mutex);
4048-
if (!cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_CONNECT)) {
4049-
ret = -EINVAL;
4050-
goto err_unlock;
4051-
}
4056+
if (!cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_CONNECT))
4057+
return -EINVAL;
40524058

40534059
if (!id->qp) {
40544060
id_priv->qp_num = conn_param->qp_num;
@@ -4066,11 +4072,33 @@ int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
40664072
ret = -ENOSYS;
40674073
if (ret)
40684074
goto err_state;
4069-
mutex_unlock(&id_priv->handler_mutex);
40704075
return 0;
40714076
err_state:
40724077
cma_comp_exch(id_priv, RDMA_CM_CONNECT, RDMA_CM_ROUTE_RESOLVED);
4073-
err_unlock:
4078+
return ret;
4079+
}
4080+
EXPORT_SYMBOL(rdma_connect_locked);
4081+
4082+
/**
4083+
* rdma_connect - Initiate an active connection request.
4084+
* @id: Connection identifier to connect.
4085+
* @conn_param: Connection information used for connected QPs.
4086+
*
4087+
* Users must have resolved a route for the rdma_cm_id to connect with by having
4088+
* called rdma_resolve_route before calling this routine.
4089+
*
4090+
* This call will either connect to a remote QP or obtain remote QP information
4091+
* for unconnected rdma_cm_id's. The actual operation is based on the
4092+
* rdma_cm_id's port space.
4093+
*/
4094+
int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
4095+
{
4096+
struct rdma_id_private *id_priv =
4097+
container_of(id, struct rdma_id_private, id);
4098+
int ret;
4099+
4100+
mutex_lock(&id_priv->handler_mutex);
4101+
ret = rdma_connect_locked(id, conn_param);
40744102
mutex_unlock(&id_priv->handler_mutex);
40754103
return ret;
40764104
}

drivers/infiniband/ulp/iser/iser_verbs.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ static void iser_route_handler(struct rdma_cm_id *cma_id)
620620
conn_param.private_data = (void *)&req_hdr;
621621
conn_param.private_data_len = sizeof(struct iser_cm_hdr);
622622

623-
ret = rdma_connect(cma_id, &conn_param);
623+
ret = rdma_connect_locked(cma_id, &conn_param);
624624
if (ret) {
625625
iser_err("failure connecting: %d\n", ret);
626626
goto failure;

drivers/infiniband/ulp/rtrs/rtrs-clt.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1674,9 +1674,9 @@ static int rtrs_rdma_route_resolved(struct rtrs_clt_con *con)
16741674
uuid_copy(&msg.sess_uuid, &sess->s.uuid);
16751675
uuid_copy(&msg.paths_uuid, &clt->paths_uuid);
16761676

1677-
err = rdma_connect(con->c.cm_id, &param);
1677+
err = rdma_connect_locked(con->c.cm_id, &param);
16781678
if (err)
1679-
rtrs_err(clt, "rdma_connect(): %d\n", err);
1679+
rtrs_err(clt, "rdma_connect_locked(): %d\n", err);
16801680

16811681
return err;
16821682
}

drivers/nvme/host/rdma.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1890,10 +1890,10 @@ static int nvme_rdma_route_resolved(struct nvme_rdma_queue *queue)
18901890
priv.hsqsize = cpu_to_le16(queue->ctrl->ctrl.sqsize);
18911891
}
18921892

1893-
ret = rdma_connect(queue->cm_id, &param);
1893+
ret = rdma_connect_locked(queue->cm_id, &param);
18941894
if (ret) {
18951895
dev_err(ctrl->ctrl.device,
1896-
"rdma_connect failed (%d).\n", ret);
1896+
"rdma_connect_locked failed (%d).\n", ret);
18971897
goto out_destroy_queue_ib;
18981898
}
18991899

include/rdma/rdma_cm.h

+2-12
Original file line numberDiff line numberDiff line change
@@ -227,19 +227,9 @@ void rdma_destroy_qp(struct rdma_cm_id *id);
227227
int rdma_init_qp_attr(struct rdma_cm_id *id, struct ib_qp_attr *qp_attr,
228228
int *qp_attr_mask);
229229

230-
/**
231-
* rdma_connect - Initiate an active connection request.
232-
* @id: Connection identifier to connect.
233-
* @conn_param: Connection information used for connected QPs.
234-
*
235-
* Users must have resolved a route for the rdma_cm_id to connect with
236-
* by having called rdma_resolve_route before calling this routine.
237-
*
238-
* This call will either connect to a remote QP or obtain remote QP
239-
* information for unconnected rdma_cm_id's. The actual operation is
240-
* based on the rdma_cm_id's port space.
241-
*/
242230
int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param);
231+
int rdma_connect_locked(struct rdma_cm_id *id,
232+
struct rdma_conn_param *conn_param);
243233

244234
int rdma_connect_ece(struct rdma_cm_id *id, struct rdma_conn_param *conn_param,
245235
struct rdma_ucm_ece *ece);

net/rds/ib_cm.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -956,9 +956,10 @@ int rds_ib_cm_initiate_connect(struct rdma_cm_id *cm_id, bool isv6)
956956
rds_ib_cm_fill_conn_param(conn, &conn_param, &dp,
957957
conn->c_proposed_version,
958958
UINT_MAX, UINT_MAX, isv6);
959-
ret = rdma_connect(cm_id, &conn_param);
959+
ret = rdma_connect_locked(cm_id, &conn_param);
960960
if (ret)
961-
rds_ib_conn_error(conn, "rdma_connect failed (%d)\n", ret);
961+
rds_ib_conn_error(conn, "rdma_connect_locked failed (%d)\n",
962+
ret);
962963

963964
out:
964965
/* Beware - returning non-zero tells the rdma_cm to destroy

0 commit comments

Comments
 (0)