Skip to contentSkip to conversation

nvmet-rdma: recheck queue state is LIVE in state lock in recv done

PATCH Superseded Ruozhu Li 2 participants 3 messages Feb 11, 2025 – Feb 13, 2025

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Ruozhu Li

author
#0 lore raw

PATCH 1/1 nvmet-rdma: recheck queue state is LIVE in state lock in recv done

The queue state checking in nvmet_rdma_recv_done is not in queue state lock.Queue state can transfer to LIVE in cm establish handler between state checking and state lock here, cause a silent drop of nvme connect cmd.
Recheck queue state whether in LIVE state in state lock to prevent this issue.

---
 drivers/nvme/target/rdma.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
drivers/nvme/target/rdma.c +9 -5
index 1afd93026f9b..9f9e51fbfe49 100644
@@ -1042,12 +1042,16 @@ static void nvmet_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc)
 		unsigned long flags;
 
 		spin_lock_irqsave(&queue->state_lock, flags);
-		if (queue->state == NVMET_RDMA_Q_CONNECTING)
-			list_add_tail(&rsp->wait_list, &queue->rsp_wait_list);
-		else
-			nvmet_rdma_put_rsp(rsp);
+		if (queue->state != NVMET_RDMA_Q_LIVE) {
+			if (queue->state == NVMET_RDMA_Q_CONNECTING)
+				list_add_tail(&rsp->wait_list, &queue->rsp_wait_list);
+			else
+				nvmet_rdma_put_rsp(rsp);
+
+			spin_unlock_irqrestore(&queue->state_lock, flags);
+			return;
+		}
 		spin_unlock_irqrestore(&queue->state_lock, flags);
-		return;
 	}
 
 	nvmet_rdma_handle_command(queue, rsp);
Signature · 1 line
2.43.0
#1 lore raw

Review static void nvmet_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc) · line 1045

Quoted 10 lines

+++ b/drivers/nvme/target/rdma.c

(quoted diff) +1 -4
@@ -1042,12 +1042,16 @@ static void nvmet_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc)
 		unsigned long flags;
 
 		spin_lock_irqsave(&queue->state_lock, flags);
-		if (queue->state == NVMET_RDMA_Q_CONNECTING)
-			list_add_tail(&rsp->wait_list, &queue->rsp_wait_list);
-		else
-			nvmet_rdma_put_rsp(rsp);
+		if (queue->state != NVMET_RDMA_Q_LIVE) {

You'll want a comment based on the commit message that the first live check is racy.

Review static void nvmet_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc) · line 1052

Quoted 7 lines
(quoted diff) +7 -0
+			if (queue->state == NVMET_RDMA_Q_CONNECTING)
+				list_add_tail(&rsp->wait_list, &queue->rsp_wait_list);
+			else
+				nvmet_rdma_put_rsp(rsp);
+
+			spin_unlock_irqrestore(&queue->state_lock, flags);
+			return;

This gets a bit messy now. What about a struture like this instead?

drivers/nvme/target/rdma.c +19 -11
index 1afd93026f9b..ae6a123ac63f 100644
@@ -996,6 +996,23 @@ static void nvmet_rdma_handle_command(struct nvmet_rdma_queue *queue,
 	nvmet_req_complete(&cmd->req, status);
 }
 
+static bool nvmet_rdma_recv_not_live(struct nvmet_rdma_queue *queue,
+		struct nvmet_rdma_rsp *rsp)
+{
+	unsigned long flags;
+	bool ret = true;
+
+	spin_lock_irqsave(&queue->state_lock, flags);
+	if (queue->state == NVMET_RDMA_Q_LIVE)
+		ret = false;
+	else if (queue->state == NVMET_RDMA_Q_CONNECTING)
+		list_add_tail(&rsp->wait_list, &queue->rsp_wait_list);
+	else
+		nvmet_rdma_put_rsp(rsp);
+	spin_unlock_irqrestore(&queue->state_lock, flags);
+	return ret;
+}
+
 static void nvmet_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc)
 {
 	struct nvmet_rdma_cmd *cmd =
@@ -1038,18 +1055,9 @@ static void nvmet_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc)
 	rsp->n_rdma = 0;
 	rsp->invalidate_rkey = 0;
 
-	if (unlikely(queue->state != NVMET_RDMA_Q_LIVE)) {
-		unsigned long flags;
-
-		spin_lock_irqsave(&queue->state_lock, flags);
-		if (queue->state == NVMET_RDMA_Q_CONNECTING)
-			list_add_tail(&rsp->wait_list, &queue->rsp_wait_list);
-		else
-			nvmet_rdma_put_rsp(rsp);
-		spin_unlock_irqrestore(&queue->state_lock, flags);
+	if (unlikely(queue->state != NVMET_RDMA_Q_LIVE) &&
+	    nvmet_rdma_recv_not_live(queue, rsp))
 		return;
-	}
-
 	nvmet_rdma_handle_command(queue, rsp);
 }
 
author
#2 lore raw
Quoted 4 lines · Christoph Hellwig
Quoted 4 lines

+++ b/drivers/nvme/target/rdma.c

(quoted diff) +0 -0
@@ -1042,12 +1042,16 @@ static void nvmet_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc)
              unsigned long flags;
 
Quoted 8 lines

drivers/nvme/target/rdma.c static void nvmet_rdma_recv_done(struct ib_cq *cq, struct ib_wc *wc)

Quoted 6 lines
drivers/nvme/target/rdma.c +1 -4
              spin_lock_irqsave(&queue->state_lock, flags);
-             if (queue->state == NVMET_RDMA_Q_CONNECTING)
-                     list_add_tail(&rsp->wait_list, &queue->rsp_wait_list);
-             else
-                     nvmet_rdma_put_rsp(rsp);
+             if (queue->state != NVMET_RDMA_Q_LIVE) {

You'll want a comment based on the commit message that the first live check is racy.

Okay, I will add a comment in next version.

Quoted 10 lines

drivers/nvme/target/rdma.c static void nvmet_rdma_handle_command(struct nvmet_rdma_queue *queue,

Quoted 8 lines
drivers/nvme/target/rdma.c +7 -0
+                     if (queue->state == NVMET_RDMA_Q_CONNECTING)
+                             list_add_tail(&rsp->wait_list, &queue->rsp_wait_list);
+                     else
+                             nvmet_rdma_put_rsp(rsp);
+
+                     spin_unlock_irqrestore(&queue->state_lock, flags);
+                     return;

This gets a bit messy now. What about a struture like this instead?

Looks good to me, I will retest it and send another version soon.

Thanks.