Skip to contentSkip to conversation

nvmet-tcp: fix a segmentation fault during io parsing error

PATCH Superseded [email protected] 2 participants 3 messages Mar 25, 2021 – Mar 26, 2021

Subsystems (2)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nvmet-tcp: fix a segmentation fault during io parsing error

From: Elad Grupi <[email protected]>

In case there is an io that contains inline data and it goes to parsing error flow, command response will free command and iov before clearing the data on the socket buffer.
This will delay the command response until receive flow is completed.

Fixes 872d26a391da ("nvmet-tcp: add NVMe over TCP target driver")
---
 drivers/nvme/target/tcp.c | 42 +++++++++++++++++++++++++++++++++------
 1 file changed, 36 insertions(+), 6 deletions(-)
drivers/nvme/target/tcp.c +36 -6
index 70cc507d1565..f10fa2b5aaeb 100644
@@ -154,6 +154,7 @@ static struct workqueue_struct *nvmet_tcp_wq;
 static const struct nvmet_fabrics_ops nvmet_tcp_ops;
 static void nvmet_tcp_free_cmd(struct nvmet_tcp_cmd *c);
 static void nvmet_tcp_finish_cmd(struct nvmet_tcp_cmd *cmd);
+static void nvmet_tcp_queue_response(struct nvmet_req *req);
 
 static inline u16 nvmet_tcp_cmd_tag(struct nvmet_tcp_queue *queue,
 		struct nvmet_tcp_cmd *cmd)
@@ -526,6 +527,12 @@ static void nvmet_tcp_queue_response(struct nvmet_req *req)
 		container_of(req, struct nvmet_tcp_cmd, req);
 	struct nvmet_tcp_queue	*queue = cmd->queue;
 
+	if (unlikely((cmd->flags & NVMET_TCP_F_INIT_FAILED) &&
+			nvmet_tcp_has_inline_data(cmd))) {
+		/* fail the cmd when we finish processing the inline data */
+		return;
+	}
+
 	llist_add(&cmd->lentry, &queue->resp_list);
 	queue_work_on(queue_cpu(queue), nvmet_tcp_wq, &cmd->queue->io_work);
 }
@@ -702,6 +709,17 @@ static int nvmet_tcp_try_send_one(struct nvmet_tcp_queue *queue,
 			return 0;
 	}
 
+	if (unlikely((cmd->flags & NVMET_TCP_F_INIT_FAILED) &&
+			nvmet_tcp_has_data_in(cmd) &&
+			nvmet_tcp_has_inline_data(cmd))) {
+		/*
+		 * wait for inline data before processing the response
+		 * so the iov will not be freed
+		 */
+		queue->snd_cmd = NULL;
+		goto done_send;
+	}
+
 	if (cmd->state == NVMET_TCP_SEND_DATA_PDU) {
 		ret = nvmet_try_send_data_pdu(cmd);
 		if (ret <= 0)
@@ -1103,9 +1121,14 @@ static int nvmet_tcp_try_recv_data(struct nvmet_tcp_queue *queue)
 		return 0;
 	}
 
-	if (!(cmd->flags & NVMET_TCP_F_INIT_FAILED) &&
-	    cmd->rbytes_done == cmd->req.transfer_len) {
-		cmd->req.execute(&cmd->req);
+	if (cmd->rbytes_done == cmd->req.transfer_len) {
+		if (unlikely(cmd->flags & NVMET_TCP_F_INIT_FAILED))
+			nvmet_tcp_queue_response(&cmd->req);
+		else {
+			if (unlikely(cmd == &queue->connect))
+				nvmet_tcp_executing_connect_cmd(queue);
+			cmd->req.execute(&cmd->req);
+		}
 	}
 
 	nvmet_prepare_receive_pdu(queue);
@@ -1143,9 +1166,16 @@ static int nvmet_tcp_try_recv_ddgst(struct nvmet_tcp_queue *queue)
 		goto out;
 	}
 
-	if (!(cmd->flags & NVMET_TCP_F_INIT_FAILED) &&
-	    cmd->rbytes_done == cmd->req.transfer_len)
-		cmd->req.execute(&cmd->req);
+	if (cmd->rbytes_done == cmd->req.transfer_len) {
+		if (unlikely(cmd->flags & NVMET_TCP_F_INIT_FAILED))
+			nvmet_tcp_queue_response(&cmd->req);
+		else {
+			if (unlikely(cmd == &queue->connect))
+				nvmet_tcp_executing_connect_cmd(queue);
+			cmd->req.execute(&cmd->req);
+		}
+	}
+
 	ret = 0;
 out:
 	nvmet_prepare_receive_pdu(queue);
Signature · 7 lines
2.18.2


_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme
#1 lore raw

drivers/nvme/target/tcp.c static struct workqueue_struct *nvmet_tcp_wq; · line 157

Quoted 9 lines · Elad Grupi
drivers/nvme/target/tcp.c +1 -0
index 70cc507d1565..f10fa2b5aaeb 100644
@@ -154,6 +154,7 @@ static struct workqueue_struct *nvmet_tcp_wq;
 static const struct nvmet_fabrics_ops nvmet_tcp_ops;
 static void nvmet_tcp_free_cmd(struct nvmet_tcp_cmd *c);
 static void nvmet_tcp_finish_cmd(struct nvmet_tcp_cmd *cmd);
+static void nvmet_tcp_queue_response(struct nvmet_req *req);

Do we need declare it here?

drivers/nvme/target/tcp.c static int nvmet_tcp_try_recv_data(struct nvmet_tcp_queue *queue) · line 1129

Quoted 13 lines
drivers/nvme/target/tcp.c +6 -3
@@ -1103,9 +1121,14 @@ static int nvmet_tcp_try_recv_data(struct nvmet_tcp_queue *queue)
 		return 0;
 	}
 
-	if (!(cmd->flags & NVMET_TCP_F_INIT_FAILED) &&
-	    cmd->rbytes_done == cmd->req.transfer_len) {
-		cmd->req.execute(&cmd->req);
+	if (cmd->rbytes_done == cmd->req.transfer_len) {
+		if (unlikely(cmd->flags & NVMET_TCP_F_INIT_FAILED))
+			nvmet_tcp_queue_response(&cmd->req);
+		else {
+			if (unlikely(cmd == &queue->connect))
+				nvmet_tcp_executing_connect_cmd(queue);

Is this in somewhere not yet upstream? I did not find nvmet_tcp_executing_connect_cmd on upstream (5.12-rc4).

drivers/nvme/target/tcp.c static int nvmet_tcp_try_recv_data(struct nvmet_tcp_queue *queue) · line 1131

Quoted 2 lines
drivers/nvme/target/tcp.c +2 -0
+			cmd->req.execute(&cmd->req);
+		}

Hi Elad,
The patch looks ok to me except these 2 question.

Thanks,
Hou

_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme

#2 lore raw

Correct. Fixed your comments

Thanks,
Elad

-----Original Message-----
From: Hou Pu <[email protected]>
Sent: Friday, 26 March 2021 13:27
To: Grupi, Elad
Cc: [email protected]; [email protected]; [email protected]
Subject: RE: [PATCH] nvmet-tcp: fix a segmentation fault during io parsing error

[EXTERNAL EMAIL]

Quoted 9 lines · Elad Grupi
drivers/nvme/target/tcp.c +0 -0
index 70cc507d1565..f10fa2b5aaeb 100644
@@ -154,6 +154,7 @@ static struct workqueue_struct *nvmet_tcp_wq;  

static const struct nvmet_fabrics_ops nvmet_tcp_ops; static void nvmet_tcp_free_cmd(struct nvmet_tcp_cmd *c); static void nvmet_tcp_finish_cmd(struct nvmet_tcp_cmd *cmd); +static void nvmet_tcp_queue_response(struct nvmet_req *req);

Do we need declare it here?

drivers/nvme/target/tcp.c static int nvmet_tcp_try_recv_data(struct nvmet_tcp_queue *queue) · line 1129

Quoted 13 lines
drivers/nvme/target/tcp.c +6 -3
@@ -1103,9 +1121,14 @@ static int nvmet_tcp_try_recv_data(struct nvmet_tcp_queue *queue)
 		return 0;
 	}
 
-	if (!(cmd->flags & NVMET_TCP_F_INIT_FAILED) &&
-	    cmd->rbytes_done == cmd->req.transfer_len) {
-		cmd->req.execute(&cmd->req);
+	if (cmd->rbytes_done == cmd->req.transfer_len) {
+		if (unlikely(cmd->flags & NVMET_TCP_F_INIT_FAILED))
+			nvmet_tcp_queue_response(&cmd->req);
+		else {
+			if (unlikely(cmd == &queue->connect))
+				nvmet_tcp_executing_connect_cmd(queue);

Is this in somewhere not yet upstream? I did not find nvmet_tcp_executing_connect_cmd on upstream (5.12-rc4).

drivers/nvme/target/tcp.c static int nvmet_tcp_try_recv_data(struct nvmet_tcp_queue *queue) · line 1131

Quoted 2 lines
drivers/nvme/target/tcp.c +2 -0
+			cmd->req.execute(&cmd->req);
+		}

Hi Elad,
The patch looks ok to me except these 2 question.

Thanks,
Hou
_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme