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 60D6455E4C; Tue, 9 Jul 2024 11:32:54 +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=1720524774; cv=none; b=IrGbqkhJwlWYrWofarPhUq/0Q2QB2ZkMtiohHlAULGhYAMds+TOcGNnkemn72OHvX3T35sUCbPTUN9L2FCGSthXvb3mEq/hAzzFGNG5X1ar8pOCX7z9F7Klrur2y41q/cNZEcKn6bc98RWSiUydAZbZSYxK+QEK8WxFAZZEB2c8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720524774; c=relaxed/simple; bh=7G+rLntam5nD6eJLh1wefQkkQLNOl/3xY7csM4HaN9Q=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fFxUZUqBpgr5FIcoE1QNsdUvtkmC9e7L/iqoy7laMzJuXJE3ONlqBclOQ8w8bpK/46Vz8wJCtEAAMN0FXt7Z3FGK5z09kPnR32F+w3SgRa4PCO9kYLeVPM/u8FEHu/QY63BxUBcxuUuhCoHNGSPMrlDNjWC8zcvpIjtVtxXeVP8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=ZRaYH4Ko; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="ZRaYH4Ko" Received: by smtp.kernel.org (Postfix) with ESMTPSA id DB71FC3277B; Tue, 9 Jul 2024 11:32:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1720524774; bh=7G+rLntam5nD6eJLh1wefQkkQLNOl/3xY7csM4HaN9Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ZRaYH4KoTlog4t3MkuMOmWSWJkXjuMBiexz92HGF/WJ4IwUfrt/zYxtnmVayCkoo+ vvsDgimlbha/+QoUJK6tCOPOERzvebXVTA3iaxLz8kASqD/LxPxOAan+A9KdT/WQ83 ACFsThg1MoZMOKgNvGZ7dUgxSZV+oCTdgQvuzAU0= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Alex Turin , Sagi Grimberg , Christoph Hellwig , Keith Busch , Sasha Levin Subject: [PATCH 6.1 097/102] nvmet: fix a possible leak when destroy a ctrl during qp establishment Date: Tue, 9 Jul 2024 13:11:00 +0200 Message-ID: <20240709110655.140794928@linuxfoundation.org> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240709110651.353707001@linuxfoundation.org> References: <20240709110651.353707001@linuxfoundation.org> User-Agent: quilt/0.67 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.1-stable review patch. If anyone has any objections, please let me know. ------------------ From: Sagi Grimberg [ Upstream commit c758b77d4a0a0ed3a1292b3fd7a2aeccd1a169a4 ] In nvmet_sq_destroy we capture sq->ctrl early and if it is non-NULL we know that a ctrl was allocated (in the admin connect request handler) and we need to release pending AERs, clear ctrl->sqs and sq->ctrl (for nvme-loop primarily), and drop the final reference on the ctrl. However, a small window is possible where nvmet_sq_destroy starts (as a result of the client giving up and disconnecting) concurrently with the nvme admin connect cmd (which may be in an early stage). But *before* kill_and_confirm of sq->ref (i.e. the admin connect managed to get an sq live reference). In this case, sq->ctrl was allocated however after it was captured in a local variable in nvmet_sq_destroy. This prevented the final reference drop on the ctrl. Solve this by re-capturing the sq->ctrl after all inflight request has completed, where for sure sq->ctrl reference is final, and move forward based on that. This issue was observed in an environment with many hosts connecting multiple ctrls simoutanuosly, creating a delay in allocating a ctrl leading up to this race window. Reported-by: Alex Turin Signed-off-by: Sagi Grimberg Reviewed-by: Christoph Hellwig Signed-off-by: Keith Busch Signed-off-by: Sasha Levin --- drivers/nvme/target/core.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/drivers/nvme/target/core.c b/drivers/nvme/target/core.c index 7b74926c50f9b..d2954406b2297 100644 --- a/drivers/nvme/target/core.c +++ b/drivers/nvme/target/core.c @@ -805,6 +805,15 @@ void nvmet_sq_destroy(struct nvmet_sq *sq) percpu_ref_exit(&sq->ref); nvmet_auth_sq_free(sq); + /* + * we must reference the ctrl again after waiting for inflight IO + * to complete. Because admin connect may have sneaked in after we + * store sq->ctrl locally, but before we killed the percpu_ref. the + * admin connect allocates and assigns sq->ctrl, which now needs a + * final ref put, as this ctrl is going away. + */ + ctrl = sq->ctrl; + if (ctrl) { /* * The teardown flow may take some time, and the host may not -- 2.43.0