Skip to content

Commit d114c6f

Browse files
committed
RDMA/cma: Add missing locking to rdma_accept()
In almost all cases rdma_accept() is called under the handler_mutex by ULPs from their handler callbacks. The one exception was ucma which did not get the handler_mutex. To improve the understand-ability of the locking scheme obtain the mutex for ucma as well. This improves how ucma works by allowing it to directly use handler_mutex for some of its internal locking against the handler callbacks intead of the global file->mut lock. There does not seem to be a serious bug here, other than a DISCONNECT event can be delivered concurrently with accept succeeding. Link: https://lore.kernel.org/r/20200818120526.702120-7-leon@kernel.org Signed-off-by: Leon Romanovsky <leonro@mellanox.com> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
1 parent 95fe510 commit d114c6f

3 files changed

Lines changed: 35 additions & 7 deletions

File tree

‎drivers/infiniband/core/cma.c‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4154,14 +4154,15 @@ static int cma_send_sidr_rep(struct rdma_id_private *id_priv,
41544154
int __rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param,
41554155
const char *caller)
41564156
{
4157-
struct rdma_id_private *id_priv;
4157+
struct rdma_id_private *id_priv =
4158+
container_of(id, struct rdma_id_private, id);
41584159
int ret;
41594160

4160-
id_priv = container_of(id, struct rdma_id_private, id);
4161+
lockdep_assert_held(&id_priv->handler_mutex);
41614162

41624163
rdma_restrack_set_task(&id_priv->res, caller);
41634164

4164-
if (!cma_comp(id_priv, RDMA_CM_CONNECT))
4165+
if (READ_ONCE(id_priv->state) != RDMA_CM_CONNECT)
41654166
return -EINVAL;
41664167

41674168
if (!id->qp && conn_param) {
@@ -4214,6 +4215,24 @@ int __rdma_accept_ece(struct rdma_cm_id *id, struct rdma_conn_param *conn_param,
42144215
}
42154216
EXPORT_SYMBOL(__rdma_accept_ece);
42164217

4218+
void rdma_lock_handler(struct rdma_cm_id *id)
4219+
{
4220+
struct rdma_id_private *id_priv =
4221+
container_of(id, struct rdma_id_private, id);
4222+
4223+
mutex_lock(&id_priv->handler_mutex);
4224+
}
4225+
EXPORT_SYMBOL(rdma_lock_handler);
4226+
4227+
void rdma_unlock_handler(struct rdma_cm_id *id)
4228+
{
4229+
struct rdma_id_private *id_priv =
4230+
container_of(id, struct rdma_id_private, id);
4231+
4232+
mutex_unlock(&id_priv->handler_mutex);
4233+
}
4234+
EXPORT_SYMBOL(rdma_unlock_handler);
4235+
42174236
int rdma_notify(struct rdma_cm_id *id, enum ib_event_type event)
42184237
{
42194238
struct rdma_id_private *id_priv;

‎drivers/infiniband/core/ucma.c‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,16 +1162,20 @@ static ssize_t ucma_accept(struct ucma_file *file, const char __user *inbuf,
11621162

11631163
if (cmd.conn_param.valid) {
11641164
ucma_copy_conn_param(ctx->cm_id, &conn_param, &cmd.conn_param);
1165-
mutex_lock(&file->mut);
11661165
mutex_lock(&ctx->mutex);
1166+
rdma_lock_handler(ctx->cm_id);
11671167
ret = __rdma_accept_ece(ctx->cm_id, &conn_param, NULL, &ece);
1168-
mutex_unlock(&ctx->mutex);
1169-
if (!ret)
1168+
if (!ret) {
1169+
/* The uid must be set atomically with the handler */
11701170
ctx->uid = cmd.uid;
1171-
mutex_unlock(&file->mut);
1171+
}
1172+
rdma_unlock_handler(ctx->cm_id);
1173+
mutex_unlock(&ctx->mutex);
11721174
} else {
11731175
mutex_lock(&ctx->mutex);
1176+
rdma_lock_handler(ctx->cm_id);
11741177
ret = __rdma_accept_ece(ctx->cm_id, NULL, NULL, &ece);
1178+
rdma_unlock_handler(ctx->cm_id);
11751179
mutex_unlock(&ctx->mutex);
11761180
}
11771181
ucma_put_ctx(ctx);

‎include/rdma/rdma_cm.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,8 @@ int rdma_listen(struct rdma_cm_id *id, int backlog);
253253
int __rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param,
254254
const char *caller);
255255

256+
void rdma_lock_handler(struct rdma_cm_id *id);
257+
void rdma_unlock_handler(struct rdma_cm_id *id);
256258
int __rdma_accept_ece(struct rdma_cm_id *id, struct rdma_conn_param *conn_param,
257259
const char *caller, struct rdma_ucm_ece *ece);
258260

@@ -270,6 +272,9 @@ int __rdma_accept_ece(struct rdma_cm_id *id, struct rdma_conn_param *conn_param,
270272
* In the case of error, a reject message is sent to the remote side and the
271273
* state of the qp associated with the id is modified to error, such that any
272274
* previously posted receive buffers would be flushed.
275+
*
276+
* This function is for use by kernel ULPs and must be called from under the
277+
* handler callback.
273278
*/
274279
#define rdma_accept(id, conn_param) \
275280
__rdma_accept((id), (conn_param), KBUILD_MODNAME)

0 commit comments

Comments
 (0)