Skip to content

Commit 6a8824a

Browse files
committed
RDMA/cm: Allow ib_send_cm_sidr_rep() to be done under lock
The first thing ib_send_cm_sidr_rep() does is obtain the lock, so use the usual unlocked wrapper, locked actor pattern here. Get rid of the cm_reject_sidr_req() wrapper so each call site can call the locked or unlocked version as required. This avoids a sketchy lock/unlock sequence (which could allow state to change) during cm_destroy_id(). Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Leon Romanovsky <[email protected]> Signed-off-by: Jason Gunthorpe <[email protected]>
1 parent 81ddb41 commit 6a8824a

File tree

1 file changed

+28
-30
lines changed
  • drivers/infiniband/core

1 file changed

+28
-30
lines changed

drivers/infiniband/core/cm.c

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ EXPORT_SYMBOL(ibcm_reject_msg);
8383
struct cm_id_private;
8484
static void cm_add_one(struct ib_device *device);
8585
static void cm_remove_one(struct ib_device *device, void *client_data);
86+
static int cm_send_sidr_rep_locked(struct cm_id_private *cm_id_priv,
87+
struct ib_cm_sidr_rep_param *param);
8688
static int cm_send_dreq_locked(struct cm_id_private *cm_id_priv,
8789
const void *private_data, u8 private_data_len);
8890
static int cm_send_drep_locked(struct cm_id_private *cm_id_priv,
@@ -830,16 +832,6 @@ static struct cm_id_private * cm_insert_remote_sidr(struct cm_id_private
830832
return NULL;
831833
}
832834

833-
static void cm_reject_sidr_req(struct cm_id_private *cm_id_priv,
834-
enum ib_cm_sidr_status status)
835-
{
836-
struct ib_cm_sidr_rep_param param;
837-
838-
memset(&param, 0, sizeof param);
839-
param.status = status;
840-
ib_send_cm_sidr_rep(&cm_id_priv->id, &param);
841-
}
842-
843835
static struct cm_id_private *cm_alloc_id_priv(struct ib_device *device,
844836
ib_cm_handler cm_handler,
845837
void *context)
@@ -1058,8 +1050,10 @@ static void cm_destroy_id(struct ib_cm_id *cm_id, int err)
10581050
spin_unlock_irq(&cm_id_priv->lock);
10591051
break;
10601052
case IB_CM_SIDR_REQ_RCVD:
1053+
cm_send_sidr_rep_locked(cm_id_priv,
1054+
&(struct ib_cm_sidr_rep_param){
1055+
.status = IB_SIDR_REJECT });
10611056
spin_unlock_irq(&cm_id_priv->lock);
1062-
cm_reject_sidr_req(cm_id_priv, IB_SIDR_REJECT);
10631057
break;
10641058
case IB_CM_REQ_SENT:
10651059
case IB_CM_MRA_REQ_RCVD:
@@ -3640,7 +3634,9 @@ static int cm_sidr_req_handler(struct cm_work *work)
36403634
cm_id_priv->id.service_id);
36413635
if (!listen_cm_id_priv) {
36423636
spin_unlock_irq(&cm.lock);
3643-
cm_reject_sidr_req(cm_id_priv, IB_SIDR_UNSUPPORTED);
3637+
ib_send_cm_sidr_rep(&cm_id_priv->id,
3638+
&(struct ib_cm_sidr_rep_param){
3639+
.status = IB_SIDR_UNSUPPORTED });
36443640
goto out; /* No match. */
36453641
}
36463642
refcount_inc(&listen_cm_id_priv->refcount);
@@ -3694,50 +3690,52 @@ static void cm_format_sidr_rep(struct cm_sidr_rep_msg *sidr_rep_msg,
36943690
param->private_data, param->private_data_len);
36953691
}
36963692

3697-
int ib_send_cm_sidr_rep(struct ib_cm_id *cm_id,
3698-
struct ib_cm_sidr_rep_param *param)
3693+
static int cm_send_sidr_rep_locked(struct cm_id_private *cm_id_priv,
3694+
struct ib_cm_sidr_rep_param *param)
36993695
{
3700-
struct cm_id_private *cm_id_priv;
37013696
struct ib_mad_send_buf *msg;
3702-
unsigned long flags;
37033697
int ret;
37043698

3699+
lockdep_assert_held(&cm_id_priv->lock);
3700+
37053701
if ((param->info && param->info_length > IB_CM_SIDR_REP_INFO_LENGTH) ||
37063702
(param->private_data &&
37073703
param->private_data_len > IB_CM_SIDR_REP_PRIVATE_DATA_SIZE))
37083704
return -EINVAL;
37093705

3710-
cm_id_priv = container_of(cm_id, struct cm_id_private, id);
3711-
spin_lock_irqsave(&cm_id_priv->lock, flags);
3712-
if (cm_id->state != IB_CM_SIDR_REQ_RCVD) {
3713-
ret = -EINVAL;
3714-
goto error;
3715-
}
3706+
if (cm_id_priv->id.state != IB_CM_SIDR_REQ_RCVD)
3707+
return -EINVAL;
37163708

37173709
ret = cm_alloc_msg(cm_id_priv, &msg);
37183710
if (ret)
3719-
goto error;
3711+
return ret;
37203712

37213713
cm_format_sidr_rep((struct cm_sidr_rep_msg *) msg->mad, cm_id_priv,
37223714
param);
37233715
ret = ib_post_send_mad(msg, NULL);
37243716
if (ret) {
3725-
spin_unlock_irqrestore(&cm_id_priv->lock, flags);
37263717
cm_free_msg(msg);
37273718
return ret;
37283719
}
3729-
cm_id->state = IB_CM_IDLE;
3730-
spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3731-
3732-
spin_lock_irqsave(&cm.lock, flags);
3720+
cm_id_priv->id.state = IB_CM_IDLE;
37333721
if (!RB_EMPTY_NODE(&cm_id_priv->sidr_id_node)) {
37343722
rb_erase(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
37353723
RB_CLEAR_NODE(&cm_id_priv->sidr_id_node);
37363724
}
3737-
spin_unlock_irqrestore(&cm.lock, flags);
37383725
return 0;
3726+
}
37393727

3740-
error: spin_unlock_irqrestore(&cm_id_priv->lock, flags);
3728+
int ib_send_cm_sidr_rep(struct ib_cm_id *cm_id,
3729+
struct ib_cm_sidr_rep_param *param)
3730+
{
3731+
struct cm_id_private *cm_id_priv =
3732+
container_of(cm_id, struct cm_id_private, id);
3733+
unsigned long flags;
3734+
int ret;
3735+
3736+
spin_lock_irqsave(&cm_id_priv->lock, flags);
3737+
ret = cm_send_sidr_rep_locked(cm_id_priv, param);
3738+
spin_unlock_irqrestore(&cm_id_priv->lock, flags);
37413739
return ret;
37423740
}
37433741
EXPORT_SYMBOL(ib_send_cm_sidr_rep);

0 commit comments

Comments
 (0)