Linux RDMA and InfiniBand development
 help / color / mirror / Atom feed
* [PATCH v2] IB/iser: Fix blocking connect hang and endpoint double free
@ 2026-11-24  9:01 Ziyu Zhang
  2026-11-24  9:12 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Ziyu Zhang @ 2026-11-24  9:01 UTC (permalink / raw)
  To: sagi, mgurtovoy, jgg, leon
  Cc: linux-rdma, linux-kernel, baijiaju1990, ziyuzhang201

iser_connect() waits for up_completion while holding
iser_conn->state_mutex.  The RDMA CM callback that advances the
connection state machine needs the same mutex, so a blocking connect
can never finish establishing and the wait only returns when a signal
arrives.  The caller then sits there with rx_queue_mutex held, which
stalls the iscsi netlink path for the whole daemon.

Dropping the mutex around the wait is not enough on its own:
up_completion is only completed on RDMA_CM_EVENT_ESTABLISHED, so an
address, route, connect or reject error still leaves the waiter asleep
forever; complete it from iser_connect_error() as well.  The cleanup
events are the other way an attempt can end without an establishment:
DISCONNECTED, ADDR_CHANGE, TIMEWAIT_EXIT and DEVICE_REMOVAL reach
iser_cleanup_handler() with the connection still ISER_CONN_PENDING,
where iser_conn_terminate() does nothing because it only acts on an
established connection.  Terminate a PENDING connection there and wake
the waiter, leaving the established-connection cleanup unchanged.
iscsi_iser_ep_poll() must then look at the connection state when the
completion fires, otherwise a failed attempt is reported as a
successful connection.

Finally, the failure paths left the connection in ISER_CONN_PENDING or
ISER_CONN_TERMINATING.  iser_conn_release() destroys iser_conn->ep when
the state is not DOWN, and iscsi_iser_ep_connect() destroys the same
endpoint again on the error return, so the endpoint was freed twice.
Terminate the connection and mark it DOWN under the state mutex instead,
so the endpoint keeps a single owner, and so that no handler can keep
operating on a connection that is being torn down.

DOWN is terminal: iser_connect() drops state_mutex before the cma_id is
destroyed, so a late CM error event can still reach iser_connect_error().
That handler used to move the state to TERMINATING unconditionally, so
iser_conn_release() destroyed the endpoint first and
iscsi_iser_ep_connect() destroyed it again when iser_connect() returned.
Leave a DOWN connection alone there.

Fixes: 504130c039f9 ("IB/iser: Protect iser state machine with a mutex")
Fixes: 9a3119e4b787 ("IB/iser: Release the iscsi endpoint if ep_disconnect wasn't called")
Cc: stable@vger.kernel.org
Signed-off-by: Ziyu Zhang <ziyuzhang201@gmail.com>

---
Changes in v2:
- Wake up the blocking waiter on connection errors and on cleanup events
  received while the connection is PENDING, including device removal.
- Check the return value of wait_for_completion_interruptible() and
  propagate signal interruption errors.
- Terminate the connection and set its state to DOWN under state_mutex
  on connect failure, leaving endpoint destruction to the caller.
- Keep DOWN terminal in iser_connect_error() so a late CM error cannot
  trigger endpoint destruction in iser_conn_release().
- Check the connection state after a positive completion result in
  iscsi_iser_ep_poll() to distinguish successful and failed connections.
- Expand the commit message to explain the failure wakeups and endpoint
  cleanup ordering.

v1: https://lore.kernel.org/linux-rdma/20261123144440.65441-1-ziyuzhang201@gmail.com/

 drivers/infiniband/ulp/iser/iscsi_iser.c | 11 +++++-
 drivers/infiniband/ulp/iser/iser_verbs.c | 50 +++++++++++++++++++++++-
 2 files changed, 58 insertions(+), 3 deletions(-)

diff --git a/drivers/infiniband/ulp/iser/iscsi_iser.c b/drivers/infiniband/ulp/iser/iscsi_iser.c
index 7df441685780..47c264cb9aff 100644
--- a/drivers/infiniband/ulp/iser/iscsi_iser.c
+++ b/drivers/infiniband/ulp/iser/iscsi_iser.c
@@ -843,7 +843,16 @@ static int iscsi_iser_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
 	rc = wait_for_completion_interruptible_timeout(&iser_conn->up_completion,
 						       msecs_to_jiffies(timeout_ms));
 	/* if conn establishment failed, return error code to iscsi */
-	if (rc == 0) {
+	if (rc > 0) {
+		/*
+		 * up_completion is also signalled when the connection attempt
+		 * failed, don't report that as a successful connection.
+		 */
+		mutex_lock(&iser_conn->state_mutex);
+		if (iser_conn->state != ISER_CONN_UP)
+			rc = -1;
+		mutex_unlock(&iser_conn->state_mutex);
+	} else if (rc == 0) {
 		mutex_lock(&iser_conn->state_mutex);
 		if (iser_conn->state == ISER_CONN_TERMINATING ||
 		    iser_conn->state == ISER_CONN_DOWN)
diff --git a/drivers/infiniband/ulp/iser/iser_verbs.c b/drivers/infiniband/ulp/iser/iser_verbs.c
index 55fe68e5b837..f57d71e3d448 100644
--- a/drivers/infiniband/ulp/iser/iser_verbs.c
+++ b/drivers/infiniband/ulp/iser/iser_verbs.c
@@ -480,7 +480,18 @@ static void iser_connect_error(struct rdma_cm_id *cma_id)
 
 	lockdep_assert_held(&iser_conn->state_mutex);
 
+	/*
+	 * A failed iser_connect() moves the connection to DOWN and drops
+	 * state_mutex before the cma_id is destroyed.  A late error event
+	 * must not move it back: iser_conn_release() would then destroy
+	 * the endpoint that iscsi_iser_ep_connect() destroys as well.
+	 */
+	if (iser_conn->state == ISER_CONN_DOWN)
+		return;
+
 	iser_conn->state = ISER_CONN_TERMINATING;
+	/* Wake up a blocking iser_connect() waiting for the result */
+	complete(&iser_conn->up_completion);
 }
 
 static void iser_calc_scsi_params(struct iser_conn *iser_conn,
@@ -652,6 +663,19 @@ static void iser_cleanup_handler(struct rdma_cm_id *cma_id,
 	struct iser_conn *iser_conn = cma_id->context;
 
 	lockdep_assert_held(&iser_conn->state_mutex);
+
+	/*
+	 * A cleanup event during connection setup ends the attempt before
+	 * it reaches ISER_CONN_UP.  iser_conn_terminate() only acts on an
+	 * established connection, so wake up iser_connect() and
+	 * iscsi_iser_ep_poll() here.  A late event after a failed connect()
+	 * finds the connection DOWN and leaves it alone.
+	 */
+	if (iser_conn->state == ISER_CONN_PENDING) {
+		iser_conn->state = ISER_CONN_TERMINATING;
+		complete(&iser_conn->up_completion);
+	}
+
 	/*
 	 * We are not guaranteed that we visited disconnected_handler
 	 * by now, call it here to be safe that we handle CM drep
@@ -778,7 +802,21 @@ int iser_connect(struct iser_conn *iser_conn, struct sockaddr *src_addr,
 	}
 
 	if (!non_blocking) {
-		wait_for_completion_interruptible(&iser_conn->up_completion);
+		int ret;
+
+		/*
+		 * The connection state machine is driven from the RDMA CM
+		 * callback, which needs state_mutex: don't hold it while
+		 * waiting for the connection to be established or to fail.
+		 */
+		mutex_unlock(&iser_conn->state_mutex);
+		ret = wait_for_completion_interruptible(&iser_conn->up_completion);
+		mutex_lock(&iser_conn->state_mutex);
+
+		if (ret) {
+			err = ret;
+			goto connect_failure;
+		}
 
 		if (iser_conn->state != ISER_CONN_UP) {
 			err =  -EIO;
@@ -795,8 +833,16 @@ int iser_connect(struct iser_conn *iser_conn, struct sockaddr *src_addr,
 id_failure:
 	ib_conn->cma_id = NULL;
 addr_failure:
-	iser_conn->state = ISER_CONN_DOWN;
 connect_failure:
+	/*
+	 * The connection never made it to ISER_CONN_UP.  Terminate it in
+	 * case the wait raced with a successful establishment, and mark it
+	 * DOWN so iser_conn_release() leaves iser_conn->ep alone: on this
+	 * path the endpoint is still owned by iscsi_iser_ep_connect(),
+	 * which destroys it when we return an error.
+	 */
+	iser_conn_terminate(iser_conn);
+	iser_conn->state = ISER_CONN_DOWN;
 	mutex_unlock(&iser_conn->state_mutex);
 	iser_conn_release(iser_conn);
 	return err;
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] IB/iser: Fix blocking connect hang and endpoint double free
  2026-11-24  9:01 [PATCH v2] IB/iser: Fix blocking connect hang and endpoint double free Ziyu Zhang
@ 2026-11-24  9:12 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-24  9:12 UTC (permalink / raw)
  To: Ziyu Zhang; +Cc: linux-rdma

> iser_connect() waits for up_completion while holding
> iser_conn->state_mutex.  The RDMA CM callback that advances the
> connection state machine needs the same mutex, so a blocking connect
> can never finish establishing and the wait only returns when a signal
> arrives.  The caller then sits there with rx_queue_mutex held, which
> stalls the iscsi netlink path for the whole daemon.
> 
> Dropping the mutex around the wait is not enough on its own:
> up_completion is only completed on RDMA_CM_EVENT_ESTABLISHED, so an
> address, route, connect or reject error still leaves the waiter asleep
> forever; complete it from iser_connect_error() as well.  The cleanup
> events are the other way an attempt can end without an establishment:
> DISCONNECTED, ADDR_CHANGE, TIMEWAIT_EXIT and DEVICE_REMOVAL reach
> iser_cleanup_handler() with the connection still ISER_CONN_PENDING,
> where iser_conn_terminate() does nothing because it only acts on an
> [ ... ]
> 
> Fixes: 504130c039f9 ("IB/iser: Protect iser state machine with a mutex")
> Fixes: 9a3119e4b787 ("IB/iser: Release the iscsi endpoint if ep_disconnect wasn't called")
> Cc: stable@vger.kernel.org
> Signed-off-by: Ziyu Zhang <ziyuzhang201@gmail.com>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261124090121.77115-1-ziyuzhang201@gmail.com?part=1

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-24  9:12 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-24  9:01 [PATCH v2] IB/iser: Fix blocking connect hang and endpoint double free Ziyu Zhang
2026-11-24  9:12 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox