Received: by 2002:ab2:6d45:0:b0:1fb:d597:ff75 with SMTP id d5csp278427lqr; Wed, 5 Jun 2024 06:03:48 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWdhRwmiCqV1ujZXg5x6fz79o9JV20pplMEVs4oRgEQmrzt7OW2m3KiJGde7pYiTd+17ULwsM2XqTwh1vNsIynsj+HpPCVPd3sBQgkSxQ== X-Google-Smtp-Source: AGHT+IFB5RDwvS10l7l/5GpLyEfVOvcfYJ3H6+JZNbkTHPlvmZgIlxwMXgpq/DS3Gg3Rz3sGFT1u X-Received: by 2002:a05:6a00:18a0:b0:6e6:98bf:7b62 with SMTP id d2e1a72fcca58-703e594abe7mr2788252b3a.8.1717592628647; Wed, 05 Jun 2024 06:03:48 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717592628; cv=pass; d=google.com; s=arc-20160816; b=WLHZwW3qhnmWCe1XSrcf5QOYeAPROKNzOaBHHGbY9ZJUdyJwrWLvlPMqtzPelBpLU+ q8zVjCgKX54or8BwefTdB5ePD9SvLIl9KOxG6LcY1njGLPHFfV4ezA25u8ON16NdPW04 2sNHveQ15bKKyEHV/XVvvasXAWYmnBlSOPqMsuR8I5OvoC6q7jqfvWpEk9ONrsEOG2AU k3bqgpGNNvDzWOFztj75UEsIvBjc247KIVluCLOtmRJHfCab8aPgxsufQu86McXqdRtS GXldkn4sL6rRaiQl6f4foNVBrOHZaDAskgVz1B7lC0//trTaSXfrNl95TjnDTPrCVSf9 rESg== 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=nMtsk21kwsKYBuA4DAWn+yjyCAHbAMmkdaoIGGbRS3s=; fh=iav3B1CAOEy6iCNWh6y5lIIjxc95kB2tL9iN7vqMeAE=; b=na7rHxz1KJIo8Ddz+x+Y89C/XiTtLOsY4ZSCDdRO9JNet65FncJbAjDiIaYYwfHWwi Gup/31nBEBJCm4A6ViPW9pyTHHz+B/bmzgYJYy8CEAyb1j7x9mYd/r8JYn9WhzR5jv70 CNqsa1vAaFG89hR93oMuBk42zEWpKwmHhlcP/6xF68ygQ48kLRL+ucY5OeAvabcK6fGg rVxRmLrLiXmiY0v1wKiR11Iq4psN3Svh1oYS2Lf5tYzgJgqpTGvaLdodlVYyEsXDfs+7 SCNLp8VL+hKC3NVItD8vydRnFrvcvVjySOd99iCylJawg/b+T7sxA1E9q/xow1iREyHp azeQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=BUHN7vbw; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-202522-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-202522-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 41be03b00d2f7-6c35b40c868si851040a12.557.2024.06.05.06.03.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Jun 2024 06:03:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-202522-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=BUHN7vbw; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-202522-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-202522-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 DBF59288D0C for ; Wed, 5 Jun 2024 12:46:27 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D22EB195981; Wed, 5 Jun 2024 12:05:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="BUHN7vbw" 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 069EC1CC711; Wed, 5 Jun 2024 12:05:11 +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=1717589111; cv=none; b=ZPuHRiWwj1KQkwPqqOwxT+pvpDupQg9kkSTw087GlQLshNVrY8hw2KlEKNIig7kiyqdiiX0yU28TTPR1dX4hmsx9iGWG6kdSIQcjI2+C4WUZWYdLrDcpD9WzosTHMcPpLW0tfh6zFbi/Yd8aSooF2swZ57URjYKQ08/iooqP3/E= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717589111; c=relaxed/simple; bh=1RbxT/6OSTDCO1lrHIY3cQWVCBAg91gJZtNuWLxq+Lg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=LmSYbienipbDes15XdYOaA7OretGO8ThbOEBg6SvVOhiJTToGgBc1fa35xqYGgfk/TUyRUar2N3WfZ8vreYgORCkfZaBCSJwx2jGMrncsAAeRzruIdLicM7pahwjuLSugBpCHgbxLkYXcplkLkRL7MwgOmHZIU+zt8+38PUWkEE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=BUHN7vbw; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id DC0BBC3277B; Wed, 5 Jun 2024 12:05:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1717589110; bh=1RbxT/6OSTDCO1lrHIY3cQWVCBAg91gJZtNuWLxq+Lg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BUHN7vbwEFuKf6tNBBko4yWH1H0+PpdN1QsivhfTDoxVm6wbW1Xqg5o3vuyd6GPk4 zII0JXKvyswJXsrwrGBwwlyxobJ3cMkWzECIkLtfbwF+kggE2k6apUIC9Vtpa742fE D0rADZjYuAluI495+QrockfjIjeqgFqfbPaV1odWqax8a1orKHIfbx6tmcAa9PV+vd +AlCdhISaqceQh9WDZkKsMLix1VkRrU5DnA/QVW1zbvlFjjW21+4VhdThkA9KRHiW1 0UfZyquT2XeAFY+utcMeMlqrxFA1indcSORhiYVZxTZ+5Z3A8LyGcyPJ8IjsxDriAb jGCTAWjXi6Y2A== 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.1 09/14] nvmet: fix a possible leak when destroy a ctrl during qp establishment Date: Wed, 5 Jun 2024 08:04:42 -0400 Message-ID: <20240605120455.2967445-9-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240605120455.2967445-1-sashal@kernel.org> References: <20240605120455.2967445-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.1.92 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 3235baf7cc6b1..c7c8dedf1f47b 100644 --- a/drivers/nvme/target/core.c +++ b/drivers/nvme/target/core.c @@ -802,6 +802,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