From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 6D8CFC25B76 for ; Wed, 5 Jun 2024 12:04:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=usn/IEZqsUKt+AxlTdVvP5AEnoZhccyZivpkNfLZGCg=; b=rq8FN5AFM/1QrvlG2+Fd4NmDuV m8ZuQUk3hfm+RnZZ/5ik2buyMzXIn2IqGk/+EjZSobLn78VOUJK0/4a4jk76KUpOqqhH5r4qqwDGl RBXHbA2ZeMXb5p2vRr4Qhx0XLGy/levfimZ+FkTaxolCIDM14sTxB0UsV/rCtOcMKygYmt9R998Hd Xm9j0tKr5OdzeQYGEi+A4lQpNrtZp6dTu4zipexC+gSD0X1XryhfaTShjQx1RJJGJuEkjNY7rOX7o MOvgwwyOlOa9zgKNeLqjXm2q9Al6qzDvDcddsATpKG4Dj7PyTiWLSQEeYx+1rCwP7xX+glganjYhx b11IQXKA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sEpNb-00000005rAC-1h2f; Wed, 05 Jun 2024 12:04:35 +0000 Received: from sin.source.kernel.org ([2604:1380:40e1:4800::1]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sEpNU-00000005r4o-379L for linux-nvme@lists.infradead.org; Wed, 05 Jun 2024 12:04:33 +0000 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 0B0E8CE1774; Wed, 5 Jun 2024 12:04:27 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 49663C32781; Wed, 5 Jun 2024 12:04:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1717589066; bh=a0IUHi3QPBEIuScy9rVloZzudGAPwQlvdVMXjKCB25w=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jlaVDT7Vnx33FFktYLKVgLJwoll0aGmLLM18terZNtVwAzZjOffaNghzuqtAQcTiE v5R6M8PzALFAlfLZu1Jia1ecBlDjm+FAWWCBXmhKKzR8itZ9n48yQrcAPgVtIo/6Fk oxyLJk3AUSkiR4PSjhTHoZLwwWkJBT/JVpndwUoc64UjhDq/13EuN6GRoVM3qj7yhP v1l/YeeYbvFyA2f1I0SsgM9Klv3C7uCbnoKXkRpiEXg6jfTv1KF3SApM+dXdMJ73Ew jtwePPSoC1D41O1YgGVcMVja0uNGqYXhi9VuzM95AoR4l62TuN86GziA8Ct1k1lnX4 +Q+2jsljOkRYw== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Sagi Grimberg , Alex Turin , Christoph Hellwig , Keith Busch , Sasha Levin , kch@nvidia.com, linux-nvme@lists.infradead.org Subject: [PATCH AUTOSEL 6.6 10/18] nvmet: fix a possible leak when destroy a ctrl during qp establishment Date: Wed, 5 Jun 2024 08:03:49 -0400 Message-ID: <20240605120409.2967044-10-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240605120409.2967044-1-sashal@kernel.org> References: <20240605120409.2967044-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore X-stable-base: Linux 6.6.32 Content-Transfer-Encoding: 8bit X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240605_050429_402987_D9F15F26 X-CRM114-Status: GOOD ( 15.36 ) X-BeenThere: linux-nvme@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "Linux-nvme" Errors-To: linux-nvme-bounces+linux-nvme=archiver.kernel.org@lists.infradead.org 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 3935165048e74..8af930e05d96c 100644 --- a/drivers/nvme/target/core.c +++ b/drivers/nvme/target/core.c @@ -803,6 +803,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