From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 47CCB17EB90; Tue, 7 May 2024 23:12:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715123529; cv=none; b=hQJtZd289TlLV+1FSXn/izSNpE43WYTOXe6iVmjjwKWfWoE8ScLffu5m6qwU9LoQe7EIgR+0KWsrdCzWjwXudLbM97513o8BPIvkjVTZjv0CsqgcLfnw/qW4U3P72UxbLizSCyyZVgVVyZCvv1se3JPjx579vSyhg2XlAtO+Yq8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715123529; c=relaxed/simple; bh=+b3UhOorUYRN3icKNmfSHrThvhesb+DJyS5oQUGZx7Y=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fWPJxTUoauvQraoMBlMSTAtqDjOg/S0uQvy4X0q0AhOMbeNM0/cDgM0e8wSrVYJRWER5wcYKh/V+mz6NvH3laJKcbMrTlymB87lcW20XqU+q35/xpgEuFMufaKZWySgT1zX4ZHed7oGeWP6S9KW3PG3CFsVuldudZ0nXWv2wURo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=RAACnHwy; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="RAACnHwy" Received: by smtp.kernel.org (Postfix) with ESMTPSA id D5A7DC4AF17; Tue, 7 May 2024 23:12:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1715123528; bh=+b3UhOorUYRN3icKNmfSHrThvhesb+DJyS5oQUGZx7Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RAACnHwykqX7TWvNA3geIRqF6tNCo/NboGtiI27Voiz6xkQeHb19xkaRHw5qNApCg s47rCceLEsdYHeOcHqV+G7OT2vXngmOxaHUdyD/3PhSPg46ef1kpwWRl4CAqB79bfM iriA5GttDAoOMIZIaMGVKSZGEd+EQcZDaO4ohAM5CXAS+kuQHVwBr+Ql9pNrrGT7zo wv667yKQ6Upade7YheBWevMQrWYx3k3hnYA4El1ZbeZmHSdtIV/W8H5gT08zjr266p S5qiuT4nbLlA5e1W44sTm420/F4VpoxcpzqCHGSGpmViST4PQBKeLT+oxvVKvmZeV2 dRCQZlViEfXsw== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Sagi Grimberg , Yi Zhang , Christoph Hellwig , Keith Busch , Sasha Levin , kch@nvidia.com, linux-nvme@lists.infradead.org Subject: [PATCH AUTOSEL 6.6 41/43] nvmet-tcp: fix possible memory leak when tearing down a controller Date: Tue, 7 May 2024 19:10:02 -0400 Message-ID: <20240507231033.393285-41-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240507231033.393285-1-sashal@kernel.org> References: <20240507231033.393285-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore X-stable-base: Linux 6.6.30 Content-Transfer-Encoding: 8bit From: Sagi Grimberg [ Upstream commit 6825bdde44340c5a9121f6d6fa25cc885bd9e821 ] When we teardown the controller, we wait for pending I/Os to complete (sq->ref on all queues to drop to zero) and then we go over the commands, and free their command buffers in case they are still fetching data from the host (e.g. processing nvme writes) and have yet to take a reference on the sq. However, we may miss the case where commands have failed before executing and are queued for sending a response, but will never occur because the queue socket is already down. In this case we may miss deallocating command buffers. Solve this by freeing all commands buffers as nvmet_tcp_free_cmd_buffers is idempotent anyways. Reported-by: Yi Zhang Tested-by: Yi Zhang Signed-off-by: Sagi Grimberg Reviewed-by: Christoph Hellwig Signed-off-by: Keith Busch Signed-off-by: Sasha Levin --- drivers/nvme/target/tcp.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/drivers/nvme/target/tcp.c b/drivers/nvme/target/tcp.c index 8e5d547aa16cb..3d302815c6f36 100644 --- a/drivers/nvme/target/tcp.c +++ b/drivers/nvme/target/tcp.c @@ -324,6 +324,7 @@ static int nvmet_tcp_check_ddgst(struct nvmet_tcp_queue *queue, void *pdu) return 0; } +/* If cmd buffers are NULL, no operation is performed */ static void nvmet_tcp_free_cmd_buffers(struct nvmet_tcp_cmd *cmd) { kfree(cmd->iov); @@ -1476,13 +1477,9 @@ static void nvmet_tcp_free_cmd_data_in_buffers(struct nvmet_tcp_queue *queue) struct nvmet_tcp_cmd *cmd = queue->cmds; int i; - for (i = 0; i < queue->nr_cmds; i++, cmd++) { - if (nvmet_tcp_need_data_in(cmd)) - nvmet_tcp_free_cmd_buffers(cmd); - } - - if (!queue->nr_cmds && nvmet_tcp_need_data_in(&queue->connect)) - nvmet_tcp_free_cmd_buffers(&queue->connect); + for (i = 0; i < queue->nr_cmds; i++, cmd++) + nvmet_tcp_free_cmd_buffers(cmd); + nvmet_tcp_free_cmd_buffers(&queue->connect); } static void nvmet_tcp_release_queue_work(struct work_struct *w) -- 2.43.0