Received: by 2002:ab2:6991:0:b0:1f7:f6c3:9cb1 with SMTP id v17csp175920lqo; Tue, 7 May 2024 16:45:39 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCX5CPV0daSTXWbre0rCotksS/cuypLCkkjZw19P7CHSbWUFFPb3O9U5lZYtEB/g1KcQgjkx4b7xlXil1niRZ5sXQYk37ML+Gg/jIpIBiA== X-Google-Smtp-Source: AGHT+IHP6hKQbrzk6EaA5oa5CJ/iCaonhvssHDJfNQ1ycr0hxApa7WBJQVTodsNdqahEChjSdZfT X-Received: by 2002:a05:6a00:6309:b0:6ed:e1c:102e with SMTP id d2e1a72fcca58-6f49c2a38d4mr1117312b3a.24.1715125538989; Tue, 07 May 2024 16:45:38 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715125538; cv=pass; d=google.com; s=arc-20160816; b=FLe8P1Ga4k3dsOWIhtko9Ui1SSqTpIvA2R0KNV7WRTUejVeabh5rujLsNDa4aIdfhB D/ORwuFq6oRdjsZ9YKIKPRt8Vy3gSSPd+wu1MrYQ98vnNsMhs6L1ZB3myFVm4G1eQKJJ QoDsMJu1+RfwXUym69Ng8KIZ1SUUpB+eU7wgsTOisAFsJegMTQ2InqwkINPy67wsXn9j 8kboMGJsRGWsVFbGftWNK0s6y96LInot2cfvCxE22MxK27i0Q1AI1sX0r/qutk2dwQq1 xSrE0i9VDe48MbP3Y0ssQVLqhyXj6SGH+rnJ311/HyhT7LduvyLnNvkiDNA5Od3VIQRt oEsw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=mLIU51c+pYnojVDAV+qOaN4vueomXM+v5jMZDy/31ro=; fh=wQltoHsUwAyQ0NGjRdaPW7tqDWGtR0H6oZS/FLNYGWQ=; b=LkSoFRwKPgIEURgSSkOkhOA2Tmwj8Kw/wKQ9NaW3Tj8HETXDE6eZw8jgiRFKFgl40m StRfObt5JYU9ttMLWEdRICSYU6EZ/U77rq3uypz74VHkujZk/SMwogpHDXI6TY0EtXKp mqRgqLrzTl/A9Xd1ZcjEw0O6gF/arzt+l8hV9bk/6MPfvmcJxr2UHTOhXUt8e3qf/ZyB ZJeEOtSQUOHwfS7ctOMB7Jl/uU1RuHr6LRADjx2uW206GLtwqviWfhM31ocKnWkOHW6Y GkbEYA3jVOHBdS8ZplFG6RjJAi7YQCgtplecyqvWogWhw2mRbvydm2zh2uvoQY0zVBG5 Kk7A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=RAACnHwy; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-172437-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-172437-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id y27-20020a056a00181b00b006f45c6196d6si7921388pfa.181.2024.05.07.16.45.38 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 May 2024 16:45:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-172437-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=RAACnHwy; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-172437-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-172437-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 1B90528667F for ; Tue, 7 May 2024 23:44:06 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1FB9F8615F; Tue, 7 May 2024 23:12:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="RAACnHwy" 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 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