rdma_disconnect() needs to know whether an active connect it aborts was still waiting for the REP or whether the REP already arrived, as in the latter case the REP upcall reports the failed connect itself. Checking cm_id->state before calling ib_send_cm_rej() is racy, as ib_send_cm_rej() also succeeds in IB_CM_REP_RCVD and IB_CM_MRA_REP_SENT, so a REP arriving in between would be rejected without the caller noticing. Add ib_cm_reject_pending_connect(), which only rejects a pending active connect and returns the former state, with the check and the REJ done under cm_id_priv->lock. As cm_send_rej_locked() moves the cm_id to IB_CM_IDLE before sending the REJ, the connect counts as aborted even if sending the REJ fails. This is needed for the next patch "RDMA/cma: reject a pending IB/RoCE connect in rdma_disconnect()". Fixes: ba88a28b47a7 ("RDMA/core: Force disconnect if DREP and DREQ fail") Cc: stable@vger.kernel.org Cc: Bernard Metzler Cc: Leon Romanovsky Cc: Jason Gunthorpe Cc: Yunseong Kim Cc: Yunseong Kim Cc: Jacob Moroni Cc: Bart Van Assche Cc: Ammar Ratnani Cc: Chuck Lever Cc: Li Xiasong Cc: Trond Myklebust Cc: Trond Myklebust Cc: Anna Schumaker Cc: Jeff Layton Cc: NeilBrown Cc: Olga Kornievskaia Cc: Dai Ngo Cc: Tom Talpey Cc: Yue Haibing Cc: Zhang Changzhong Cc: Wei Yongjun Cc: Tim Menninger Cc: linux-rdma@vger.kernel.org Cc: linux-nfs@vger.kernel.org Cc: linux-cifs@vger.kernel.org Cc: linux-kernel@vger.kernel.org Signed-off-by: Stefan Metzmacher --- drivers/infiniband/core/cm.c | 34 ++++++++++++++++++++++++++++++++++ include/rdma/ib_cm.h | 17 +++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/drivers/infiniband/core/cm.c b/drivers/infiniband/core/cm.c index d16fc71ed5e6..9d43097a6ef0 100644 --- a/drivers/infiniband/core/cm.c +++ b/drivers/infiniband/core/cm.c @@ -3025,6 +3025,40 @@ int ib_send_cm_rej(struct ib_cm_id *cm_id, enum ib_cm_rej_reason reason, } EXPORT_SYMBOL(ib_send_cm_rej); +int ib_cm_reject_pending_connect(struct ib_cm_id *cm_id, + enum ib_cm_state *old_state) +{ + struct cm_id_private *cm_id_priv = + container_of(cm_id, struct cm_id_private, id); + unsigned long flags; + int ret = -EINVAL; + + spin_lock_irqsave(&cm_id_priv->lock, flags); + *old_state = cm_id->state; + switch (cm_id->state) { + case IB_CM_REQ_SENT: + case IB_CM_MRA_REQ_RCVD: + case IB_CM_REP_RCVD: + case IB_CM_MRA_REP_SENT: + /* + * cm_send_rej_locked() moves the cm_id to IB_CM_IDLE + * before it sends the REJ, so the connect is aborted + * even if sending the REJ fails, the peer then + * notices via its own timeout. + */ + cm_send_rej_locked(cm_id_priv, IB_CM_REJ_CONSUMER_DEFINED, + NULL, 0, NULL, 0); + ret = 0; + break; + default: + break; + } + spin_unlock_irqrestore(&cm_id_priv->lock, flags); + + return ret; +} +EXPORT_SYMBOL(ib_cm_reject_pending_connect); + static void cm_format_rej_event(struct cm_work *work) { struct cm_rej_msg *rej_msg; diff --git a/include/rdma/ib_cm.h b/include/rdma/ib_cm.h index 4808a355de41..87071479f194 100644 --- a/include/rdma/ib_cm.h +++ b/include/rdma/ib_cm.h @@ -480,6 +480,23 @@ int ib_send_cm_rej(struct ib_cm_id *cm_id, const void *private_data, u8 private_data_len); +/** + * ib_cm_reject_pending_connect - Rejects an active connect that is not + * established yet. + * @cm_id: Connection identifier of the active side. + * @old_state: Returns the state of @cm_id before. + * + * Unlike ib_send_cm_rej() this only rejects in IB_CM_REQ_SENT, + * IB_CM_MRA_REQ_RCVD, IB_CM_REP_RCVD and IB_CM_MRA_REP_SENT, and the + * state check and the REJ are done atomically, so the caller knows + * whether a REP arrived before. @cm_id is in IB_CM_IDLE afterwards, + * even if the REJ could not be sent. + * + * Returns 0 if the connect was rejected, -EINVAL otherwise. + */ +int ib_cm_reject_pending_connect(struct ib_cm_id *cm_id, + enum ib_cm_state *old_state); + /** * ib_prepare_cm_mra - Prepares to send a message receipt acknowledgment to a * connection message in case duplicates are received. -- 2.43.0