Received: by 2002:ab2:6d45:0:b0:1fb:d597:ff75 with SMTP id d5csp263765lqr; Wed, 5 Jun 2024 05:37:10 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVhctwRgGQZdKNOWMCiWJKLUMxDNU+TbvF6RfSwNux5lwFhv9qinYHCl/A9W+5dHRal9OjaI9gUjK8gWt/KvPqOoY+IDgH3Q8IiuhmlRA== X-Google-Smtp-Source: AGHT+IG9Kgu6pqxbgLTXmEJRJHE56CTk5ic+xxJ+LVaGOq1bOSmtlGX4LDcT4/xYoDeIcORsHKMe X-Received: by 2002:a05:6102:26d4:b0:48b:abba:5d8f with SMTP id ada2fe7eead31-48c04897a19mr3519393137.19.1717591029944; Wed, 05 Jun 2024 05:37:09 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717591029; cv=pass; d=google.com; s=arc-20160816; b=Gt2kiPzRjbLgPcDzlIdfY+d9VgVbIlFijK6WTfaOtFhXLk/R5YD0Ik9WXyhApYbig5 CTJm1DbWRZCf8stQP1kuyFQm79sn+em75VZ1JU8vkz132RHqSbi036FDHtbFQPdF+sYG dbW1A1EJIkVApW3YYJIQ36t8ZFBnPekhJOkaZne9qOhZxkJoRsaIYXyJgB90nmHz26RO VJGQYF11yuguviETCT3/5BKIDgTFS8ek76b8dvsEl+XMQu1bOdYtpZ5aH5PkWj66mz37 pi9Z8Jc53FLU5bKu/BHyOMaEiJADae1OmEJwY8H30tzYqUnYNkPfZ1Wh379B1E3Kazp6 KatA== 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=qON9sl2KMlwb/Dz+4Nsf6i1Ftbuawb9EQyXRrCItspU=; fh=iav3B1CAOEy6iCNWh6y5lIIjxc95kB2tL9iN7vqMeAE=; b=0eMUvRqgtfKiB85xh9OzQWpsm4ImWoaEY/Of64O+PNUomblueMa3KRjHIH4nFe/Qls qUrcNKeNAAYumE9xKIDwuXOvvrJS/v6bgLThxBWFUTQRWphSJSmaxy+PxJynhu1rhrkB 8dzbxc3dFcweV01U1m/6Q9jBlCDcRr8VpSdDhHj8hJwG8BukUg2bT4HeV1In/LkGFxAZ NavHkWCVRzXsB7JoNYyu97TErmL1Uq/XSzfEJrGxQb+/DMxYrDz+nnZd1kpxdl+mAM1b 0epR3h5ShS9YSgAXKE8Lj1u9XWZXp3mauoXqksGuRD9fgFUgbF+hgWEOYyTb56j/12dG bSDw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ei6Z3n0o; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-202485-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-202485-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id ada2fe7eead31-48bbcff5191si2009414137.565.2024.06.05.05.37.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Jun 2024 05:37:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-202485-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ei6Z3n0o; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-202485-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-202485-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id A502E1C226F4 for ; Wed, 5 Jun 2024 12:37:09 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 19AD41C44EE; Wed, 5 Jun 2024 12:03:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="ei6Z3n0o" 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 3D2FC1A2FB4; Wed, 5 Jun 2024 12:03:37 +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=1717589017; cv=none; b=jeIdVqqdChIP1P3Jv0s+H9R0C7isr4WJsxrAv9bRnEQP+Hi06oAp41ku4TlBY2TM8ZeulrxRGRcxbkFYs12d00dZC1zUqdbT486aBFB8qAo8pzit5dOKN1/CVhUDaHG8TSNgpBEaWKN3ZwsXmDA+8SoINFGwqtuX8O5XDousB9o= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717589017; c=relaxed/simple; bh=lXZHFMoD413+IMSKUMc0OO0hxvipo7lN0LkxpOheYXg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=SCM1WXZ4TJPrlc8tKwrQl4isQHi9p+RjXcdC1oO09ShbiVtBIwgluVOhlj+xCgF4TiH/E/uBYlMSVWd3M4nsMAN/IiBXz87TOShXmxb8zQhDHR0TsN5ba3Dh/x83Kb75zPyRIBPnxImMN/ubaJQpJaZBh2/LqmCLbZM7Hy2eTOw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ei6Z3n0o; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 25F7FC4AF09; Wed, 5 Jun 2024 12:03:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1717589017; bh=lXZHFMoD413+IMSKUMc0OO0hxvipo7lN0LkxpOheYXg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ei6Z3n0oCICIaIZFYKb4caHVIHCvR1qK7hRTul3RtbJNROOepEjXUaeW7eosTWMIF EeMw3vxcHEWpK1S7iQQoy+C/wS+cG//TPH08I65VxU4En0n20d6bPrMGiXfLoZblpT TJlu/7x6sDtMcvNAGl9SmtksF844oxOuy+RIi1N8QwLhQnKUPH1vv2k8cbzuV5zfoU 44LQwdCsJNTRfcWB/mFEP7T2wNSfoSjJy9N7AjEPf/kUgdPE7YQ6zQyEgBmJvaHsV/ y3qbvfhdd/3dEsGe2useiDgfKxgCxPybIwd0JizIQRvKPyZ/a8XCfSRXUgTsOELKCb dL6YITLNIF6Sw== 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.8 10/18] nvmet: fix a possible leak when destroy a ctrl during qp establishment Date: Wed, 5 Jun 2024 08:03:00 -0400 Message-ID: <20240605120319.2966627-10-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240605120319.2966627-1-sashal@kernel.org> References: <20240605120319.2966627-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.12 Content-Transfer-Encoding: 8bit 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 7a6b3d37cca70..ab7a6177f3831 100644 --- a/drivers/nvme/target/core.c +++ b/drivers/nvme/target/core.c @@ -806,6 +806,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