Received: by 2002:ab2:6d45:0:b0:1fb:d597:ff75 with SMTP id d5csp279576lqr; Wed, 5 Jun 2024 06:05:10 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVtujxQ1MoHTappzyGumKf/j7HwTjqitkTyeYQNQtjvrd1ZBdl0+YQwx0fsgcJywsGlGI5fjRAd32do0AIs1Daap768v6iAFPRpqnm7Ug== X-Google-Smtp-Source: AGHT+IGxtes/RvdT6YbdbZmi1bx45SK/C/FdVMO7VHwtCLdhCay7LVbTEzATTEaVGVaKsiO8Q/bc X-Received: by 2002:a05:6871:8a4:b0:250:8060:8cd0 with SMTP id 586e51a60fabf-2512275199emr2796287fac.34.1717592709725; Wed, 05 Jun 2024 06:05:09 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717592709; cv=pass; d=google.com; s=arc-20160816; b=pnPNuelMKONzkeDtatoz6jnM01Y13HkJSlaZsG4nb9wuWUIbpvNXhZxi55QYgwg6BG DC/EiCIu8qmBqeziJ/Zd01dllMXzw/MDqt6TvaECj1UgP1lNaMtIix9bBYsJKNNFwVip lm7Q1pSvZ+e3cn4qL8q5+d16gGR3RxmG6KcolC1rovCTa5UYjyoFCqw9ocWLe7m+n710 oAcq/rfW1BAtIBo83zFloKggFK19s0baPiJLcJDNfzLoqTqU3fHEWU1yvnfzNVajEkH2 E52qk28NpxN0aqFP/J/o7UZDP4+RZGxU9Auevt801lWmlw0SvuUKi/rXLoubSNPVVdlu /kaQ== 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=icRdAs3+JbAxP9pPPp+MxWZYif+CXdy+xKMXW+fOgfk=; fh=iav3B1CAOEy6iCNWh6y5lIIjxc95kB2tL9iN7vqMeAE=; b=liz3MdkFN/Wr/MgVPM26g0iXn4qMKCv77mx+dY7a7VzkGUDNXAE/S1JZ+mb4OaiKsW 7UWk35B4+IOvZI2RerqzWGLccTGhtNG1V4cZ/uFmoUyPpBMVW7ul2ecRQMNBal2wNDF8 EfNeP/oXrw6KNSjZ5LSEmFgHoVUL3n1ZugJ5+LdzjhPq2qANV02C3RTf8iAzW/n88orp sO2uBawYWQRixTkrcbIYCgDvfvi1FoO0u9nF5JI6PewaHGInWP+VKUuzbDDqEPwmmLJI aVwreYro+Fx5+V6MF4sQe0Mr7rv9IE7OvGw/fcyP+O6RI4eYuQSVPQkDklruJAglm4M0 8jCg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=r+oCLBIx; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-202547-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-202547-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. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id 586e51a60fabf-25084f45a61si4183890fac.53.2024.06.05.06.05.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Jun 2024 06:05:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-202547-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=r+oCLBIx; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-202547-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-202547-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 55864292BD7 for ; Wed, 5 Jun 2024 12:53:59 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A49DD1AC42E; Wed, 5 Jun 2024 12:06:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="r+oCLBIx" 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 C68C71D1570; Wed, 5 Jun 2024 12:06:07 +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=1717589167; cv=none; b=H/a4b7XHQQm/nQUdlIUwI9GUNAf+qK2n5gWiwGUx9fMX7VR7gunExSvYTdokhojGEzf2UQxNJ+Cxevb17xDTE+WFcXEgSpnIeU1vs4yfWoRjTgBpSu1am0s+6iz7c+4YN8rAUro/0q1Z0sUUU2FR1u3XAjxcH7O7mGPCkopCQBE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717589167; c=relaxed/simple; bh=0Q8eaGMyTpjn7ZtOcjGlTiOA+wWWTJCHK9F63KK14yo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Ba5rLARaNuBBo0uD88noSjwW6MH2wLA8jA1hT9Q2jMmd6169j0JVWgnD+mceKb18jq3hU6o0Kzz7932oM7I+hPCF1bnkF4xnJk/+hFkCBjhzauJDJ++qWV4xiVfqGbBoLQAA3LVzG4kRaUQc0vaHLTmOWOUiLJwrpS5/nLIwE/0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=r+oCLBIx; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 62F16C32781; Wed, 5 Jun 2024 12:06:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1717589167; bh=0Q8eaGMyTpjn7ZtOcjGlTiOA+wWWTJCHK9F63KK14yo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=r+oCLBIxUQKg9xV6MmTht1tGp/E+zETplYS3BHj5314VDAo2F6/0krI1gOn2qFvg+ JS/narjJIzjKoaPUNSAB7SR7yvjEDfOwXXcxyEG1HuVqvksoT/IRyr2FQAP901nfXp jQ0z6hzvQ9kOR3T0pSI9KKCRqfs0iTplBrGK7XJbBaBKyP4D9S4UWHSxJnf5thJDHu imSU3alepTZ8G20oDNHc1IegD192eQ4HV4mVl0khoxT5c5oCqaaJh5G6FG4mQOq8xO jqaLgHzt9zIqTAstDwkPK73LXrc7vjepjEWQcG8NJJU7qK95lAYCDSVQjttBnmSr/y BK9TGnDqq4lew== 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 5.10 7/8] nvmet: fix a possible leak when destroy a ctrl during qp establishment Date: Wed, 5 Jun 2024 08:05:50 -0400 Message-ID: <20240605120554.2968012-7-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240605120554.2968012-1-sashal@kernel.org> References: <20240605120554.2968012-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 5.10.218 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 59109eb8e8e46..a04bb02c1251b 100644 --- a/drivers/nvme/target/core.c +++ b/drivers/nvme/target/core.c @@ -795,6 +795,15 @@ void nvmet_sq_destroy(struct nvmet_sq *sq) wait_for_completion(&sq->free_done); percpu_ref_exit(&sq->ref); + /* + * 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