Received: by 2002:ab2:6991:0:b0:1f7:f6c3:9cb1 with SMTP id v17csp172085lqo; Tue, 7 May 2024 16:34:27 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWceqIYtEh2Tco/yNtxe933FLqhfotlgbAR9EykhsM6+kqRLU0/XXo7jYtDm7Yji/HFeQEwulc0Ld2aqWtKNlJpfLUomxYsf4/yL61PUQ== X-Google-Smtp-Source: AGHT+IHehWDdnFcQB1tCraMwtXR+HgMUFJje7gcdY9uaLBoFORSYlSj1Rn8VhcE9h9jSvQSNLoKU X-Received: by 2002:a05:6870:219e:b0:23c:3c37:a4ef with SMTP id 586e51a60fabf-2409807a988mr1194053fac.29.1715124867313; Tue, 07 May 2024 16:34:27 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715124867; cv=pass; d=google.com; s=arc-20160816; b=MovW/8lls8eMlfb4jewEo2YYv2vHALlHC70yZ+TzzpaGL9jYcm+cUGKmzuF3/NsuFG xpe5h8XuKQOL9+p86vPZBxH8dGxpOM6hglnr/xNA/tcu3fjSk/UlztnC8YBNu6PE96AS yQ00HPpQnyP4IQUxSV2fThhFsp9zbXvWuHW4z+cMHZKxgLFNLGHM5fwopPjdAcop5aVA nSVEPdY2LQ/FraCwdh+3IvnBt6UuBX1peOO5J5YnmkCe/U3+PjP/dbXHjjAvVZQe6o2M mWtnozWJrwwdXMOF/HUiXOjwNEmmt0RtmjCIsOd9xg9yOR79DIt8jJTTADhJPhNrDx0y /JOA== 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=+ZJmeDjUlEqATDvE8TCy4l1WEwVnSFjV/t9fQ6PcWAc=; fh=wQltoHsUwAyQ0NGjRdaPW7tqDWGtR0H6oZS/FLNYGWQ=; b=nR4mhvIJZK5Shy6Z3NzhJ9edqJpr3SAAmoII2FYoKNomkW1qh2edcqh97J6u1dMxNF FefG/Lw7AVmGrtE7A1EG0xqny3BCo7fNdpM6ykAdGIq/Uan+CJbOxlxol2QYC+oS/o2M c1cspNv4tqGLbzdCDu8e9xflDSWNtwX3mfBPOkzTG/N2duQxvLJAjYQQ7JL2S+2cA4xh YJVom6ahKdB6sUAAHYUEW+wpHS+XrhrhwRslvGyFcWIzikLFX4e9XALvGN1PX5NcR40q xOSAeixezS4YBY2Dzpp25se5Ema7BT3f9mYaW6DGZoUl/ZRnVzrvZCwg50IzO020otqD ER+Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=SPo6sPJs; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-172392-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-172392-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 z11-20020a637e0b000000b0061ae5df1ab4si10728626pgc.545.2024.05.07.16.34.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 May 2024 16:34:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-172392-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=SPo6sPJs; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-172392-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-172392-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 E94D8280D4C for ; Tue, 7 May 2024 23:34:26 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A4339147C62; Tue, 7 May 2024 23:09:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="SPo6sPJs" 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 CABB31474CB; Tue, 7 May 2024 23:09:58 +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=1715123398; cv=none; b=i6e0d9s5GWDinbKRm8dAhVs56Ph3x1KHumQZt7DEgdQs2Je1EqnjF13B1LI+L6b+PNRKwYkY9zcN/zlHlkwrY4kV1wDa323QwmW4l+1AxhIROIzdG2JVYO3Xh5IEmIO7oyWRlNzwU4IeBx77svwo4XgGv0E+I79fSe5+oHdqubE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715123398; c=relaxed/simple; bh=/c+CqSrk0Nb00Vn1vKDMf5s29J0LlhfzUrJBA2Fp5rE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=DVY9cpVEtDSikD0DK+/xZYy2noD3uxTQnQCugeAv0+FAw5B2eT2Wj9rMRZ9YPcayfDkhcIo4slh9KgDo/dHCOefkuSY392aP58hMcNMq+ObhpjeAjL0itCwN/ZFR87UYIhms5w3avytUDBhRCJ40tqcLtcvKm+fpseL5Q22t6ZQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=SPo6sPJs; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id AC73FC3277B; Tue, 7 May 2024 23:09:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1715123398; bh=/c+CqSrk0Nb00Vn1vKDMf5s29J0LlhfzUrJBA2Fp5rE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SPo6sPJscregAUVo8M/Ny1zNWbcnAYDWizmBHanhR+Z4D4T2aabvCNfpNRLrPUQfY 2Qx/tiZDZmlxPt7G0tBRk2K1JD/ge4Rn/XRLqDGHyJMB4fwb6u4GLPYVRq+MgR8TDL lXbhXMNooX7DNTzC3OI0ktL1dxTU8g6EoAJgsrpmT7x8RtOwyWa/FFv57snCOTgYZP nBNeTKmwU0W+4o5+MEcYteCANLFfs7fiuxPh5LRDw0ZOh0G24FpXmUnPxXTfo7P8Xo DfphZ93nDkWzvHpj09EV53fbcfb9wgbyhHg/ck/5SZgVGWdkKYpV/lDqOqdnnlhe1U moLXTVMTB758w== 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.8 49/52] nvmet-tcp: fix possible memory leak when tearing down a controller Date: Tue, 7 May 2024 19:07:15 -0400 Message-ID: <20240507230800.392128-49-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240507230800.392128-1-sashal@kernel.org> References: <20240507230800.392128-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.8.9 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 c8655fc5aa5b8..8d4531a1606d1 100644 --- a/drivers/nvme/target/tcp.c +++ b/drivers/nvme/target/tcp.c @@ -348,6 +348,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); @@ -1580,13 +1581,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