Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp2353186pxb; Mon, 20 Sep 2021 19:51:06 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxu7oUEStwAi+4PtR8QpSvppHN7zNU+WEB34nl3vY3aWcBKJ6pPaRXOK/uWGrFebraueFU8 X-Received: by 2002:a6b:fe18:: with SMTP id x24mr8443324ioh.119.1632192665947; Mon, 20 Sep 2021 19:51:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1632192665; cv=none; d=google.com; s=arc-20160816; b=FBf5YLw0yvYz2xrAVd51CSer5Eni5Gucol1mscN+W3/mupwBnwcR1zIAZG2K+yosvi VKDf91cqRZZnbn7j49QIKQE61fAXg9OIZ5VWOO2YJ8LriamWYkoiHQHD1KkrmN5h5VoX aeeozvqYc1P4HfoMzF0W03HNDVD7iXtMruS3aWhDkiqlMcB6UIZylwRTWT4T4/aK3N6t IEYJGM3QBZQrQPU+rKyazWDOIYTDTen7OY/3vDSVjairLTHsgoY9C0qD6rTZy1zaiI6T xEunH5Agj7pc9tfxSSg7xa5nHTGeWF6EPf0sl9TUW5Fqu6I9qURQbdyPkpHpZpJau6GI iEwA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=yCVZpt14LHiZZ35olE70OQW4xTxQ3K+/jUQxKKmhrnU=; b=yQivBOWg88SqJshI1ShC2d7dn/n97/Wjhjn+ledzT63bBtUIK8mVfZCDc+TH1Phqvn w1o1WwO5KyOWW5kfDT8f6fe5SlYX8ivW6Fcxb56pzszQVTi2ZWT7I93woaLevWIfMtXq 1galSdb2OD84BtWdmFRJeD5rdnYrCPHZ33lHyobOsSYNpU8HD+v0pSzS9Y125HI5t8oh tzUAF5+pTCcx+lWovjK99mItmiSQPI35sbukfA3p/A1PDAjwgBYbUQHS1V9jLbAUbgT+ 7Flt+x/5+ZfhIoFx4I6NTpDxaBFWbfZY0AVAkYiOwEzal0I2wC5iZIwB5JvNgyjrKD7u m16g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=P4YOXej4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id k3si13836203jap.19.2021.09.20.19.50.54; Mon, 20 Sep 2021 19:51:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=P4YOXej4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1384148AbhITSqn (ORCPT + 99 others); Mon, 20 Sep 2021 14:46:43 -0400 Received: from mail.kernel.org ([198.145.29.99]:56478 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1382851AbhITSmb (ORCPT ); Mon, 20 Sep 2021 14:42:31 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 1069961AEF; Mon, 20 Sep 2021 17:32:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1632159122; bh=6IwJarFCdNzRwDXYJVptoxhyQs8prZ9bD91bftdHQCg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=P4YOXej4uxS6Z+MRj9Otv5p1Xl1alSSo7JxMCA8QQG9lGegaVw71LfeDS929DPOdi LU33Q4VyDIZIOokbZF1Qro7nJbUoXh9rf0iCKkCGqHKpvllscs24VAMsMNFLAPDP5U 22Tc7bZwlimV268UZe7uA9h/vLlJSFPH0WMeUm9w= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Daniel Wagner , Hannes Reinecke , Christoph Hellwig , Sasha Levin Subject: [PATCH 5.14 086/168] nvme: avoid race in shutdown namespace removal Date: Mon, 20 Sep 2021 18:43:44 +0200 Message-Id: <20210920163924.463133132@linuxfoundation.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210920163921.633181900@linuxfoundation.org> References: <20210920163921.633181900@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Daniel Wagner [ Upstream commit 9edceaf43050f5ba1dd7d0011bcf68a736a17743 ] When we remove the siblings entry, we update ns->head->list, hence we can't separate the removal and test for being empty. They have to be in the same critical section to avoid a race. To avoid breaking the refcounting imbalance again, add a list empty check to nvme_find_ns_head. Fixes: 5396fdac56d8 ("nvme: fix refcounting imbalance when all paths are down") Signed-off-by: Daniel Wagner Reviewed-by: Hannes Reinecke Tested-by: Hannes Reinecke Signed-off-by: Christoph Hellwig Signed-off-by: Sasha Levin --- drivers/nvme/host/core.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/drivers/nvme/host/core.c b/drivers/nvme/host/core.c index 2f0cbaba12ac..84e7cb9f1968 100644 --- a/drivers/nvme/host/core.c +++ b/drivers/nvme/host/core.c @@ -3496,7 +3496,9 @@ static struct nvme_ns_head *nvme_find_ns_head(struct nvme_subsystem *subsys, lockdep_assert_held(&subsys->lock); list_for_each_entry(h, &subsys->nsheads, entry) { - if (h->ns_id == nsid && nvme_tryget_ns_head(h)) + if (h->ns_id != nsid) + continue; + if (!list_empty(&h->list) && nvme_tryget_ns_head(h)) return h; } @@ -3821,6 +3823,10 @@ static void nvme_ns_remove(struct nvme_ns *ns) mutex_lock(&ns->ctrl->subsys->lock); list_del_rcu(&ns->siblings); + if (list_empty(&ns->head->list)) { + list_del_init(&ns->head->entry); + last_path = true; + } mutex_unlock(&ns->ctrl->subsys->lock); synchronize_rcu(); /* guarantee not available in head->list */ @@ -3840,13 +3846,6 @@ static void nvme_ns_remove(struct nvme_ns *ns) list_del_init(&ns->list); up_write(&ns->ctrl->namespaces_rwsem); - /* Synchronize with nvme_init_ns_head() */ - mutex_lock(&ns->head->subsys->lock); - if (list_empty(&ns->head->list)) { - list_del_init(&ns->head->entry); - last_path = true; - } - mutex_unlock(&ns->head->subsys->lock); if (last_path) nvme_mpath_shutdown_disk(ns->head); nvme_put_ns(ns); -- 2.30.2