Skip to contentSkip to conversation

nvmet-tcp: fix memory leak when having inflight commands on disconnect

PATCH In review [email protected] 2 participants 2 messages Jun 1, 2021 – Jun 8, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nvmet-tcp: fix memory leak when having inflight commands on disconnect

From: Elad Grupi <[email protected]>

Some nvme commands might get completed during the execution of nvmet_tcp_release_queue_work. Those commands are being added to resp_list on the queue and never processed after cancel_work_sync is done, causing memory leak of the resources allocated for those commands.

---
 drivers/nvme/target/tcp.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
drivers/nvme/target/tcp.c +26 -0
index f9f34f6caf5e..04d53b532ae2 100644
@@ -1426,6 +1426,31 @@ static void nvmet_tcp_uninit_data_in_cmds(struct nvmet_tcp_queue *queue)
 	}
 }
 
+static void nvmet_tcp_free_resp_list(struct nvmet_tcp_queue *queue)
+{
+	struct nvmet_tcp_cmd *cmd;
+	int c = 0;
+
+	nvmet_tcp_process_resp_list(queue);
+
+	list_for_each_entry(cmd, &queue->resp_send_list, entry) {
+		kfree(cmd->iov);
+		sgl_free(cmd->req.sg);
+		c++;
+	}
+
+	WARN_ON(c != queue->send_list_len);
+
+	if (queue->snd_cmd) {
+		kfree(queue->snd_cmd->iov);
+		sgl_free(queue->snd_cmd->req.sg);
+		c++;
+	}
+
+	pr_debug("qid %u send_list_len %d, free %d unprocessed commands\n",
+			queue->nvme_sq.qid, queue->send_list_len, c);
+}
+
 static void nvmet_tcp_release_queue_work(struct work_struct *w)
 {
 	struct nvmet_tcp_queue *queue =
@@ -1441,6 +1466,7 @@ static void nvmet_tcp_release_queue_work(struct work_struct *w)
 	nvmet_tcp_uninit_data_in_cmds(queue);
 	nvmet_sq_destroy(&queue->nvme_sq);
 	cancel_work_sync(&queue->io_work);
+	nvmet_tcp_free_resp_list(queue);
 	sock_release(queue->sock);
 	nvmet_tcp_free_cmds(queue);
 	if (queue->hdr_digest || queue->data_digest)
Signature · 7 lines
2.18.2


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

From: Elad Grupi <[email protected]>

Some nvme commands might get completed during the execution of nvmet_tcp_release_queue_work. Those commands are being added to resp_list on the queue and never processed after cancel_work_sync is done, causing memory leak of the resources allocated for those commands.

---
  drivers/nvme/target/tcp.c | 26 ++++++++++++++++++++++++++
  1 file changed, 26 insertions(+)
drivers/nvme/target/tcp.c +25 -0
index f9f34f6caf5e..04d53b532ae2 100644
@@ -1426,6 +1426,31 @@ static void nvmet_tcp_uninit_data_in_cmds(struct nvmet_tcp_queue *queue)
  	}
  }
 
+static void nvmet_tcp_free_resp_list(struct nvmet_tcp_queue *queue)
+{
+	struct nvmet_tcp_cmd *cmd;
+	int c = 0;
+
+	nvmet_tcp_process_resp_list(queue);
+
+	list_for_each_entry(cmd, &queue->resp_send_list, entry) {
+		kfree(cmd->iov);
+		sgl_free(cmd->req.sg);
+		c++;
+	}
+
+	WARN_ON(c != queue->send_list_len);
+
+	if (queue->snd_cmd) {
+		kfree(queue->snd_cmd->iov);
+		sgl_free(queue->snd_cmd->req.sg);
+		c++;
+	}
+
+	pr_debug("qid %u send_list_len %d, free %d unprocessed commands\n",
+			queue->nvme_sq.qid, queue->send_list_len, c);
+}
+
  static void nvmet_tcp_release_queue_work(struct work_struct *w)
  {
  	struct nvmet_tcp_queue *queue =

drivers/nvme/target/tcp.c static void nvmet_tcp_release_queue_work(struct work_struct *w) · line 1469

Quoted 4 lines
drivers/nvme/target/tcp.c +1 -0
@@ -1441,6 +1466,7 @@ static void nvmet_tcp_release_queue_work(struct work_struct *w)
  	nvmet_tcp_uninit_data_in_cmds(queue);
  	nvmet_sq_destroy(&queue->nvme_sq);
  	cancel_work_sync(&queue->io_work);
+	nvmet_tcp_free_resp_list(queue);

Can't this be combined with nvme_tcp_free_cmds? Maybe consolidate the sgl and iov free to something like nvme_tcp_free_cmd_data that will free these and make it null-free-safe?

drivers/nvme/target/tcp.c static void nvmet_tcp_release_queue_work(struct work_struct *w) · line 1472

Quoted 4 lines
drivers/nvme/target/tcp.c +0 -0
  	sock_release(queue->sock);
  	nvmet_tcp_free_cmds(queue);
  	if (queue->hdr_digest || queue->data_digest)

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