RDMA/cma: reject a pending IB/RoCE connect in rdma_disconnect()
Subsystems (6)
cover RDMA/cma: reject a pending IB/RoCE connect in rdma_disconnect()
Hi,
if rdma_disconnect() is called while an active IB/RoCE connect is still waiting for the REP, ib_send_cm_dreq() and ib_send_cm_drep() fail, as the connection is not established. Since commit ba88a28b47a7 ("RDMA/core: Force disconnect if DREP and DREQ fail") the caller then gets RDMA_CM_EVENT_DISCONNECTED for a connection that was never established, while the REQ is still pending in ib_cm. RDMA_CM_CONNECT covers both a pending and an established connection, so the rdma_cm state doesn't distinguish them.
- The first patch lets ib_send_cm_rej() cancel our outstanding REQ (or REP), otherwise a following rdma_destroy_id() waits about 76 seconds until all retries of the REQ timed out.
- The second patch adds ib_cm_reject_pending_connect(), which rejects a pending active connect and returns the former state, with the check and the REJ done atomically, so the caller knows whether a REP arrived in between.
- The third patch uses it in rdma_disconnect(): a connect still
waiting for the REP is rejected and reported as
RDMA_CM_EVENT_CONNECT_ERROR with -ECONNABORTED. If the REP is already being processed, only the REJ is sent, as the REP upcall reports the failed connect itself.
Without a QP (rdma_ucm), the caller keeps getting
RDMA_CM_EVENT_DISCONNECTED as before. If ib_cm is already idle, a
pending REJECTED or UNREACHABLE event reports the failed connect,
so no second event is forced. An established connection still
gets the forced RDMA_CM_EVENT_DISCONNECTED.
This came up in the discussion about ba88a28b47a7 with Ammar and Chuck:
https://lore.kernel.org/linux-nfs/[email protected]/
A larger series lets rdma_disconnect() abort a connection setup for iWarp as well, on top of these two patches, I'll post it later.
These patches were developed with the help of Claude (Anthropic), which was used for code analysis, writing parts of the code and the commit messages. I reviewed all changes and take full responsibility for them.
Testing: two hosts with rxe, Intel E810 (irdma) and ConnectX (mlx5) in RoCEv2 mode: SMB over RDMA (cifs.ko/ksmbd.ko) connects against a listener that never answers, aborted after about 5 seconds and by killing the mounting processes, without waiting for the REQ retries, and a user space rdma_cm test that aborts pending connects with rdma_disconnect() or by destroying the QP or the rdma_cm_id first. The established case (address removed or link down while connected) works as before.
Base: linux-rdma/for-next (a8671ee9bde1).
Stefan Metzmacher (3):
RDMA/cm: cancel the outstanding REQ or REP in ib_send_cm_rej() RDMA/cm: add ib_cm_reject_pending_connect()
RDMA/cma: reject a pending IB/RoCE connect in rdma_disconnect()
drivers/infiniband/core/cm.c | 45 +++++++++++- drivers/infiniband/core/cma.c | 110 ++++++++++++++++++++++++++++ drivers/infiniband/core/cma_trace.h | 1 + include/rdma/ib_cm.h | 17 +++++ 4 files changed, 171 insertions(+), 2 deletions(-)
Signature · 1 line
2.43.0
PATCH 1/3 RDMA/cm: cancel the outstanding REQ or REP in ib_send_cm_rej()
ib_send_cm_rej() is allowed in IB_CM_REQ_SENT and IB_CM_MRA_REQ_RCVD, but it only moves the cm_id to IB_CM_IDLE and sends the REJ, while our REQ stays outstanding. It is retried and its send buffer keeps a reference on the cm_id until all retries timed out, so a following ib_destroy_cm_id(), which finds IB_CM_IDLE and doesn't cancel anything, waits that long in cm_destroy_id(), about 76 seconds with the rdma_cm defaults. The peer also keeps getting the REQ after the REJ.
This is needed for the following patch "RDMA/cma: reject a pending IB/RoCE connect in rdma_disconnect()", which aborts a connect without an answer from the peer, when the rdma_cm_id is destroyed afterwards:
cm_destroy_id+0x23b/0x630 [ib_cm] ib_destroy_cm_id+0x10/0x20 [ib_cm] _destroy_id+0xd8/0x390 [rdma_cm] destroy_id_handler_unlock+0x56/0xb0 [rdma_cm] rdma_destroy_id+0x21/0x30 [rdma_cm] smbdirect_socket_destroy_sync+0x48f/0x9c0 [smbdirect]
cm_destroy_id() already cancelled the outstanding REQ (and our REP in IB_CM_REP_SENT and IB_CM_MRA_REP_RCVD) before calling cm_send_rej_locked(), so move that into cm_send_rej_locked() for all callers.
--- drivers/infiniband/core/cm.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/infiniband/core/cm.c +9 -2
@@ -1095,7 +1095,6 @@ static void cm_destroy_id(struct ib_cm_id *cm_id, int err)break ;case IB_CM_REQ_SENT :case IB_CM_MRA_REQ_RCVD : -ib_cancel_mad (cm_id_priv->msg); cm_send_rej_locked(cm_id_priv, IB_CM_REJ_TIMEOUT, &cm_id_priv->id.device->node_guid, sizeof(cm_id_priv->id.device->node_guid),
@@ -1113,7 +1112,6 @@ static void cm_destroy_id(struct ib_cm_id *cm_id, int err)break ;case IB_CM_REP_SENT :case IB_CM_MRA_REP_RCVD : -ib_cancel_mad (cm_id_priv->msg);cm_send_rej_locked (cm_id_priv,IB_CM_REJ_CONSUMER_DEFINED ,NULL ,0 ,NULL ,0 ); goto retest;
@@ -2965,6 +2963,13 @@ static int cm_send_rej_locked(struct cm_id_private *cm_id_priv,switch (state) {case IB_CM_REQ_SENT :case IB_CM_MRA_REQ_RCVD : +/* +* Our REQ is still outstanding, it would otherwise be +* retried and keep a reference on the cm_id until all +* retries timed out. +*/ +ib_cancel_mad (cm_id_priv->msg); + fallthrough;case IB_CM_REQ_RCVD :case IB_CM_MRA_REQ_SENT :case IB_CM_REP_RCVD :
@@ -2979,6 +2984,8 @@ static int cm_send_rej_locked(struct cm_id_private *cm_id_priv,break ;case IB_CM_REP_SENT :case IB_CM_MRA_REP_RCVD : +/* the same for our outstanding REP */ +ib_cancel_mad (cm_id_priv->msg);cm_enter_timewait (cm_id_priv); msg =cm_alloc_msg (cm_id_priv);if (IS_ERR (msg))
Signature · 1 line
2.43.0
PATCH 2/3 RDMA/cm: add ib_cm_reject_pending_connect()
In reply to Stefan Metzmacherrdma_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()".
--- drivers/infiniband/core/cm.c | 34 ++++++++++++++++++++++++++++++++++ include/rdma/ib_cm.h | 17 +++++++++++++++++ 2 files changed, 51 insertions(+)
drivers/infiniband/core/cm.c +34 -0
@@ -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;
include/rdma/ib_cm.h +17 -0
@@ -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.
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH 2/3] RDMA/cm: add ib_cm_reject_pending_connect()
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] RDMA/cm: unmatchable REJ sent before REP in ib_cm_reject_pending_connect() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] RDMA/cm: unmatchable REJ sent before REP in ib_cm_reject_pending_connect()
--
commit 4b27fca63a8905d689ea7a1a5659bc7e21ff263b
Author: Stefan Metzmacher <[email protected]>
RDMA/cm: add ib_cm_reject_pending_connect()
This commit adds ib_cm_reject_pending_connect() to atomically check the CM state, reject a pending active connection under cm_id_priv->lock, and return the prior state to the caller.
Quoted 23 lines
drivers/infiniband/core/cm.c +15
-0@@ -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 :
drivers/infiniband/core/cm.c int ib_send_cm_rej(struct ib_cm_id *cm_id, enum ib_cm_rej_reason reason, · line 3052
Quoted 10 lines
drivers/infiniband/core/cm.c +10
-0+/* +* 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 ;
[Severity: High]
Does sending IB_CM_REJ_CONSUMER_DEFINED with a NULL ARI in IB_CM_REQ_SENT and IB_CM_MRA_REQ_RCVD produce a REJ MAD that the passive peer cannot match?
Before a REP arrives, cm_id_priv->id.remote_id is still 0. On the passive side, cm_acquire_rejected_id() only looks up the connection by the remote CA GUID in the ARI and remote_id when the reason is IB_CM_REJ_TIMEOUT:
drivers/infiniband/core/cm.c:cm_acquire_rejected_id() {
if (IBA_GET(CM_REJ_REASON, rej_msg) == IB_CM_REJ_TIMEOUT) {
cm_id_priv = cm_find_remote_id(
*((__be64 *)IBA_GET_MEM_PTR(CM_REJ_ARI, rej_msg)),
remote_id);
} else if (IBA_GET(CM_REJ_MESSAGE_REJECTED, rej_msg) ==
CM_MSG_RESPONSE_REQ)
cm_id_priv = cm_acquire_id(
cpu_to_be32(IBA_GET(CM_REJ_REMOTE_COMM_ID, rej_msg)),
0);
else
cm_id_priv = cm_acquire_id(
cpu_to_be32(IBA_GET(CM_REJ_REMOTE_COMM_ID, rej_msg)),
remote_id);
}With IB_CM_REJ_CONSUMER_DEFINED, cm_acquire_rejected_id() takes the final else branch with CM_REJ_REMOTE_COMM_ID == 0, fails to find the cm_id, and drops the REJ. Because cm_send_rej_locked() also resets cm_id->state to IB_CM_IDLE, a later cm_destroy_id() will no longer send the IB_CM_REJ_TIMEOUT with node_guid in the ARI that it previously sent, leaving the passive peer's connection and REP retries dangling until they time out.
Should IB_CM_REQ_SENT and IB_CM_MRA_REQ_RCVD pass IB_CM_REJ_TIMEOUT and &cm_id_priv->id.device->node_guid in the ARI, matching cm_destroy_id()?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
PATCH 3/3 RDMA/cma: reject a pending IB/RoCE connect in rdma_disconnect()
In reply to Stefan MetzmacherIf rdma_disconnect() is called while an active IB/RoCE connect is still waiting for the REP, ib_send_cm_dreq() and ib_send_cm_drep() fail, as the ib_cm_id is not established. Since commit ba88a28b47a7 ("RDMA/core: Force disconnect if DREP and DREQ fail") the caller then gets RDMA_CM_EVENT_DISCONNECTED for a connection that was never established, while the REQ is still pending in ib_cm and a REP arriving later is ignored by cma without sending an RTU, so the peer only notices via its own timeouts.
Instead reject the pending connect with ib_cm_reject_pending_connect(), which checks the state and sends the REJ atomically, and report RDMA_CM_EVENT_CONNECT_ERROR with -ECONNABORTED, moving to RDMA_CM_DISCONNECT, so another rdma_disconnect() doesn't generate a second event. The QP was already moved to error, so it can be drained and destroyed.
So the caller, also in user space via rdma_ucm, gets the same kind of event as for a connect rejected by the peer.
If the REP already arrived (IB_CM_REP_RCVD or, after ib_prepare_cm_mra(), IB_CM_MRA_REP_SENT), cma_ib_handler() is processing it under the handler_mutex. With a QP also reject it, but don't queue an event: cma_rep_recv() fails, as the QP is in error or ib_send_cm_rtu() fails after the REJ, and the REP upcall reports RDMA_CM_EVENT_CONNECT_ERROR itself. Without a QP the caller got RDMA_CM_EVENT_CONNECT_RESPONSE and its rdma_accept() fails after the REJ, so it still gets RDMA_CM_EVENT_DISCONNECTED as before, which librdmacm ignores if it created the QP.
If ib_cm is already in IB_CM_IDLE while the rdma_cm_id is still in RDMA_CM_CONNECT, the connect already failed, e.g. the peer sent a REJ or our REQ timed out, and the pending RDMA_CM_EVENT_REJECTED or RDMA_CM_EVENT_UNREACHABLE reports it, so don't force a second event.
The work for the event is allocated before the REJ is sent, so that a rejected connect is always reported.
On the passive side, if rdma_disconnect() is called after the REP was sent, but before the RTU arrived, the caller still gets the forced RDMA_CM_EVENT_DISCONNECTED, as before.
The new rdma_cma:cm_abort_connect trace event shows when this happens.
--- drivers/infiniband/core/cma.c | 110 ++++++++++++++++++++++++++++ drivers/infiniband/core/cma_trace.h | 1 + 2 files changed, 111 insertions(+)
drivers/infiniband/core/cma.c +110 -0
@@ -4846,6 +4846,114 @@ static int cma_force_disconnect(struct rdma_id_private *id_priv)return 0 ; } +/* +* Called from rdma_disconnect() after the pending active connect was +* rejected locally: report it to the caller like any other failed +* connect. +*/ +static struct cma_work *cma_alloc_abort_work (void ) +{ +struct cma_work *work; + + work =kzalloc_obj (*work); +if (!work) +return NULL ; + +INIT_WORK (&work->work, cma_work_handler); +/* +* Like cma_force_disconnect() move to RDMA_CM_DISCONNECT, so +* another rdma_disconnect() doesn't generate a second event. +*/ + work->old_state =RDMA_CM_CONNECT ; + work->new_state =RDMA_CM_DISCONNECT ; + work->event.event =RDMA_CM_EVENT_CONNECT_ERROR ; + work->event.status = -ECONNABORTED ; + +return work; +} + +static void cma_queue_abort_work (struct rdma_id_private *id_priv, +struct cma_work *work) +{ +cma_id_get (id_priv); + work->id = id_priv; + +trace_cm_abort_connect (id_priv); + +queue_work (cma_wq, &work->work); +} + +/* +* Returns true if rdma_disconnect() is done with *ret, false if the +* connection is established (or closing) and needs a DREQ or DREP. +*/ +static bool cma_ib_abort_pending_connect (struct rdma_id_private *id_priv, +int *ret) +{ +struct ib_cm_id *cm_id = id_priv->cm_id.ib; +enum ib_cm_state old_state; +struct cma_work *work; + +/* +* Allocated before the REJ, so that a rejected connect is +* always reported. +*/ + work =cma_alloc_abort_work (); +if (!work) { + *ret = -ENOMEM ; +return true; + } + +trace_cm_send_rej (id_priv); +if (ib_cm_reject_pending_connect (cm_id, &old_state)) { +kfree (work); +if (old_state ==IB_CM_IDLE ) { +/* +* The connect already failed, e.g. the peer sent +* a REJ or our REQ timed out, and the pending +* REJECTED or UNREACHABLE event reports it, or +* it was already aborted. +*/ + *ret =0 ; +return true; + } +return false; + } + +switch (old_state) { +case IB_CM_REQ_SENT : +case IB_CM_MRA_REQ_RCVD : +/* +* Still waiting for the REP, so no other event is pending +* for the connect: report it as failed connect. A REP +* arriving later is dropped by ib_cm. +*/ +cma_queue_abort_work (id_priv, work); + *ret =0 ; +return true; +default : +break ; + } + +/* +* The REP arrived (IB_CM_REP_RCVD or IB_CM_MRA_REP_SENT). With a +* QP it is being processed by cma_ib_handler() under the +* handler_mutex: cma_rep_recv() fails, as the QP is in error or +* ib_send_cm_rtu() fails after the REJ, and the REP upcall +* reports RDMA_CM_EVENT_CONNECT_ERROR itself, so we must not +* queue a second event. +* +* Without a QP the caller got (or is about to get) +* RDMA_CM_EVENT_CONNECT_RESPONSE and its rdma_accept() fails +* after the REJ, so keep reporting RDMA_CM_EVENT_DISCONNECTED via +* cma_force_disconnect() as before, librdmacm ignores it if it +* created the QP. +*/ +kfree (work); + *ret = id_priv->id.qp ?0 :cma_force_disconnect (id_priv); +return true; +} +int rdma_disconnect (struct rdma_cm_id *id) {struct rdma_id_private *id_priv;
@@ -4859,6 +4967,8 @@ int rdma_disconnect(struct rdma_cm_id *id) ret =cma_modify_qp_err (id_priv);if (ret) goto out; +if (cma_ib_abort_pending_connect (id_priv, &ret)) + goto out;/* Initiate or respond to a disconnect. */ trace_cm_disconnect (id_priv);if (!ib_send_cm_dreq (id_priv->cm_id.ib,NULL ,0 )) {
drivers/infiniband/core/cma_trace.h +1 -0
@@ -62,6 +62,7 @@ DEFINE_CMA_FSM_EVENT(disconnect);DEFINE_CMA_FSM_EVENT (sent_drep);DEFINE_CMA_FSM_EVENT (sent_dreq);DEFINE_CMA_FSM_EVENT (force_disconnect); +DEFINE_CMA_FSM_EVENT (abort_connect);DEFINE_CMA_FSM_EVENT (id_destroy); TRACE_EVENT(cm_id_attach,
Signature · 1 line
2.43.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 4 potential issues to consider: - [High] RDMA/cma: unhandled IB_CM_TIMEWAIT in cma_ib_abort_pending_connect() - [Medium] RDMA/cma…
Thank you for your contribution! Sashiko AI review found 4 potential issues to consider:
- [High] RDMA/cma: unhandled IB_CM_TIMEWAIT in cma_ib_abort_pending_connect()
- [Medium] RDMA/cma: unconditional cma_work alloc in cma_ib_abort_pending_connect()
- [Medium] RDMA/cma: race in cma_ib_abort_pending_connect() drops REP event without QP
- [Low] RDMA/cma: unconditional trace_cm_send_rej() in cma_ib_abort_pending_connect()
--
commit dcca139b10a4d4a837fd675d7aac97c93bfbb127
Author: Stefan Metzmacher <[email protected]>
RDMA/cma: reject a pending IB/RoCE connect in rdma_disconnect()
This commit rejects a pending active IB/RoCE connect in rdma_disconnect() via ib_cm_reject_pending_connect() and reports RDMA_CM_EVENT_CONNECT_ERROR with -ECONNABORTED instead of forcing RDMA_CM_EVENT_DISCONNECTED.
Quoted 8 lines
drivers/infiniband/core/cma.c +0
-0@@ -4846,6 +4846,114 @@ static int cma_force_disconnect(struct rdma_id_private *id_priv)return 0 ; }
[ ... ]
Quoted 10 lines
drivers/infiniband/core/cma.c +10
-0+/* +* Returns true if rdma_disconnect() is done with *ret, false if the +* connection is established (or closing) and needs a DREQ or DREP. +*/ +static bool cma_ib_abort_pending_connect (struct rdma_id_private *id_priv, +int *ret) +{ +struct ib_cm_id *cm_id = id_priv->cm_id.ib; +enum ib_cm_state old_state; +struct cma_work *work;
drivers/infiniband/core/cma.c static int cma_force_disconnect(struct rdma_id_private *id_priv) · line 4905
Quoted 10 lines
drivers/infiniband/core/cma.c +10
-0+ +/* +* Allocated before the REJ, so that a rejected connect is +* always reported. +*/ + work =cma_alloc_abort_work (); +if (!work) { + *ret = -ENOMEM ; +return true; + }
[Severity: Medium]
Does calling cma_alloc_abort_work() unconditionally here allocate and immediately free an unused struct cma_work on every normal disconnect of an established IB/RoCE connection? If that allocation fails under memory pressure on an established connection, rdma_disconnect() returns -ENOMEM after moving the QP to IB_QPS_ERR without sending a DREQ or DREP.
Also, in the IB_CM_REP_RCVD / IB_CM_MRA_REP_SENT path without a QP below, work is freed and cma_force_disconnect() allocates a second struct cma_work after the REJ has already been sent, losing the disconnect event if the second allocation fails.
drivers/infiniband/core/cma.c
Quoted 2 lines
drivers/infiniband/core/cma.c +2
-0+ +trace_cm_send_rej (id_priv);
[Severity: Low]
Doesn't calling trace_cm_send_rej() before ib_cm_reject_pending_connect() emit a spurious cm_send_rej trace event on every normal disconnect of an established IB/RoCE connection where no REJ is sent?
Quoted 4 lines
drivers/infiniband/core/cma.c +4
-0+if (ib_cm_reject_pending_connect (cm_id, &old_state)) { +kfree (work); +if (old_state ==IB_CM_IDLE ) { +/*
drivers/infiniband/core/cma.c static int cma_force_disconnect(struct rdma_id_private *id_priv) · line 4921
Quoted 10 lines
drivers/infiniband/core/cma.c +10
-0+* The connect already failed, e.g. the peer sent +* a REJ or our REQ timed out, and the pending +* REJECTED or UNREACHABLE event reports it, or +* it was already aborted. +*/ + *ret =0 ; +return true; + } +return false; + }
[Severity: High]
What happens if the peer rejected the pending connect and cm_rej_handler() moved cm_id->state to IB_CM_TIMEWAIT instead of IB_CM_IDLE?
drivers/infiniband/core/cm.c:cm_rej_handler() {
...
case IB_CM_REQ_SENT:
case IB_CM_MRA_REQ_RCVD:
ib_cancel_mad(cm_id_priv->msg);
if (IBA_GET(CM_REJ_REASON, rej_msg) == IB_CM_REJ_STALE_CONN)
cm_enter_timewait(cm_id_priv);
else
cm_reset_to_idle(cm_id_priv);
break;
case IB_CM_DREQ_SENT:
ib_cancel_mad(cm_id_priv->msg);
fallthrough;
case IB_CM_REP_RCVD:
case IB_CM_MRA_REP_SENT:
cm_enter_timewait(cm_id_priv);
break;
...
}When a pending connect is rejected with IB_CM_REJ_STALE_CONN (or rejected in IB_CM_REP_RCVD or IB_CM_MRA_REP_SENT), cm_id->state remains in IB_CM_TIMEWAIT until the timewait timer expires. Because only old_state == IB_CM_IDLE is checked here, cma_ib_abort_pending_connect() returns false and rdma_disconnect() falls through to cma_force_disconnect(), queuing a spurious RDMA_CM_EVENT_DISCONNECTED for a connection that was never established.
In xprtrdma, xprt_rdma_connect_worker() calls rpcrdma_xprt_disconnect() -> rdma_disconnect() right after waking up from RDMA_CM_EVENT_REJECTED; in rpcrdma_cm_event_handler(), receiving the subsequent RDMA_CM_EVENT_DISCONNECTED calls rpcrdma_ep_put(ep) without a matching rpcrdma_ep_get(ep) from RDMA_CM_EVENT_ESTABLISHED, causing a reference count underflow and use-after-free of struct rpcrdma_ep.
Quoted 24 lines
drivers/infiniband/core/cma.c +24
-0+ +switch (old_state) { +case IB_CM_REQ_SENT : +case IB_CM_MRA_REQ_RCVD : +/* +* Still waiting for the REP, so no other event is pending +* for the connect: report it as failed connect. A REP +* arriving later is dropped by ib_cm. +*/ +cma_queue_abort_work (id_priv, work); + *ret =0 ; +return true; +default : +break ; + } + +/* +* The REP arrived (IB_CM_REP_RCVD or IB_CM_MRA_REP_SENT). With a +* QP it is being processed by cma_ib_handler() under the +* handler_mutex: cma_rep_recv() fails, as the QP is in error or +* ib_send_cm_rtu() fails after the REJ, and the REP upcall +* reports RDMA_CM_EVENT_CONNECT_ERROR itself, so we must not +* queue a second event. +*
drivers/infiniband/core/cma.c static int cma_force_disconnect(struct rdma_id_private *id_priv) · line 4955
Quoted 10 lines
drivers/infiniband/core/cma.c +10
-0+* Without a QP the caller got (or is about to get) +* RDMA_CM_EVENT_CONNECT_RESPONSE and its rdma_accept() fails +* after the REJ, so keep reporting RDMA_CM_EVENT_DISCONNECTED via +* cma_force_disconnect() as before, librdmacm ignores it if it +* created the QP. +*/ +kfree (work); + *ret = id_priv->id.qp ?0 :cma_force_disconnect (id_priv); +return true; +}
[Severity: Medium]
When old_state is IB_CM_REP_RCVD and id_priv->id.qp is NULL (such as for userspace rdma_ucm connections), can cma_force_disconnect() race with cma_ib_handler() and drop RDMA_CM_EVENT_CONNECT_RESPONSE?
CPU0 (cm.wq) CPU1cm_rep_handler()
lock(&cm_id_priv->lock)
cm_id->state = IB_CM_REP_RCVD
unlock(&cm_id_priv->lock)
rdma_disconnect()
cma_ib_abort_pending_connect()
ib_cm_reject_pending_connect()
old_state == IB_CM_REP_RCVD
cma_force_disconnect()
queue_work(cma_wq, &work->work)
cma_work_handler()
mutex_lock(&id_priv->handler_mutex)
id_priv->state = RDMA_CM_DISCONNECT
deliver RDMA_CM_EVENT_DISCONNECTED
mutex_unlock(&id_priv->handler_mutex)cma_ib_handler(IB_CM_REP_RECEIVED)
mutex_lock(&id_priv->handler_mutex)
state != RDMA_CM_CONNECT
goto out; <-- drops RDMA_CM_EVENT_CONNECT_RESPONSE
cm_rep_handler() sets cm_id->state to IB_CM_REP_RCVD and drops cm_id_priv->lock before cma_ib_handler() acquires id_priv->handler_mutex and calls ib_prepare_cm_mra() (which moves cm_id->state to IB_CM_MRA_REP_SENT). If cma_work_handler() runs first, cma_ib_handler() drops IB_CM_REP_RECEIVED, and because librdmacm ignores RDMA_CM_EVENT_DISCONNECTED when it manages the QP in userspace, the caller never receives RDMA_CM_EVENT_CONNECT_RESPONSE or fails in rdma_accept().
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
[email protected]
AI reviewSashiko has finished reviewing this 3-patch series:
Sashiko has finished reviewing this 3-patch series:
- Patch 1: no issues found.
- Patch 2 ("RDMA/cm: add ib_cm_reject_pending_connect()"): 1 potential issue (1 High)
- Patch 3 ("RDMA/cma: reject a pending IB/RoCE connect in rdma_d..."): 4 potential issues (1 High, 2 Medium, 1 Low)
Detailed reviews have been sent as separate replies to patches 2 and 3.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]