Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp4152044pxb; Mon, 27 Sep 2021 10:26:51 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyhGQYnrylSqH+cPOe3aa1HS23QyHXvo7J0tUEHpS8YlRTF6RMT03+RFifB3h/0MCoQeHxi X-Received: by 2002:a62:63c6:0:b0:43f:cbf9:4383 with SMTP id x189-20020a6263c6000000b0043fcbf94383mr1191618pfb.14.1632763611392; Mon, 27 Sep 2021 10:26:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1632763611; cv=none; d=google.com; s=arc-20160816; b=u5byikM9i2gz5aJSIhrxdv4g7YoD7e/RAY1ZjL40vGGhgjv/Zlzz6EjyQxvGoVgDqV g+80o1/MCgkmu47CUoanwymagxcZqtFhwpSW1rUdJeszEjtsbyYRf3OIBnfze9Nh8K+N EeRz9nSztRAu7OHt1gmgqbDkI7HqvTmuZPSlR9NcoenSULtq7WSF9vudW8nRmDp3JgI0 6seJjXyfFlzY70AvbMIgazb+Nd7jZ3DQ3XtxXqQJBrX//Ji9F8ElpLZNLJMR2CcmlqJ+ te5zll01AcFJcv1PLF0Gnpr7deir6X7bFlZUaAkNiHV9LMCjKLaU1w7qg5UXcrLXHunS 48aA== 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=ndsYpuEW7eH5XppoBDtgWT7IR/QM3986cyZS/bFi6aM=; b=mX6XSNeyyC5R6N/g4gSLh7V9gY/KI3HWxLOTsy4EGLCYlFajIKWj4qPK+5KHDBhJ6c v01bYCrSgQy/3akr8Olj9indk8ENRS76hWxnbFU+fxIIMfolZrgOnJ5nWF/sTqM1ZMGT 8TlhrkY8/m8tcdsQH4IkMtihXe+8I9Q4D6rmaGrRjdwNnQea/ujwaaCt6V2D0NpyPIQB x0g3CgMVJ+zC9tRWx8lRQUJ3bLi2kbQQfS7NcMU5PFBsT9Qbao1G2i4D9j4o8GmvPQma HM7nmeIc/oLr5h2NvHLGUV76koossQ6qrZHgwJfXNR+0NuUzOf4yF58qWhWjxf8tfH5U 3Xog== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=lTuGOtr6; 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 o190si14532234pfd.8.2021.09.27.10.26.39; Mon, 27 Sep 2021 10:26:51 -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=lTuGOtr6; 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 S236020AbhI0R11 (ORCPT + 99 others); Mon, 27 Sep 2021 13:27:27 -0400 Received: from mail.kernel.org ([198.145.29.99]:38056 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237812AbhI0RXj (ORCPT ); Mon, 27 Sep 2021 13:23:39 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 2562D613BD; Mon, 27 Sep 2021 17:15:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1632762914; bh=Mki/4vEkW7gdDuoJCnORrTBgAy84cclSLUpQDFCo/tU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lTuGOtr6Mry/rKQjHWjOWvPyW72r4oK08SY28prSCylYNekcyfO65KGzD/QhUIyle Hx94Sg2+wIGZqmAeHws4W+qNuNC8hVP8V1yNYyWtEUb7WLvp17ioIsb56A/d8y1aP3 o0rUOtF4I1yWFf+574Ho+bb/AqaR2Z4+S5Ev0kHQ= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Anton Eidelman , Christoph Hellwig , Keith Busch , Sagi Grimberg , Chaitanya Kulkarni , Damien Le Moal , Sasha Levin Subject: [PATCH 5.14 099/162] nvme: keep ctrl->namespaces ordered Date: Mon, 27 Sep 2021 19:02:25 +0200 Message-Id: <20210927170236.870374988@linuxfoundation.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210927170233.453060397@linuxfoundation.org> References: <20210927170233.453060397@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: Christoph Hellwig [ Upstream commit 298ba0e3d4af539cc37f982d4c011a0f07fca48c ] Various places in the nvme code that rely on ctrl->namespace to be ordered. Ensure that the namespae is inserted into the list at the right position from the start instead of sorting it after the fact. Fixes: 540c801c65eb ("NVMe: Implement namespace list scanning") Reported-by: Anton Eidelman Signed-off-by: Christoph Hellwig Reviewed-by: Keith Busch Reviewed-by: Sagi Grimberg Reviewed-by: Chaitanya Kulkarni Reviewed-by: Damien Le Moal Signed-off-by: Sasha Levin --- drivers/nvme/host/core.c | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/drivers/nvme/host/core.c b/drivers/nvme/host/core.c index 84e7cb9f1968..e2374319df61 100644 --- a/drivers/nvme/host/core.c +++ b/drivers/nvme/host/core.c @@ -13,7 +13,6 @@ #include #include #include -#include #include #include #include @@ -3688,15 +3687,6 @@ out_unlock: return ret; } -static int ns_cmp(void *priv, const struct list_head *a, - const struct list_head *b) -{ - struct nvme_ns *nsa = container_of(a, struct nvme_ns, list); - struct nvme_ns *nsb = container_of(b, struct nvme_ns, list); - - return nsa->head->ns_id - nsb->head->ns_id; -} - struct nvme_ns *nvme_find_get_ns(struct nvme_ctrl *ctrl, unsigned nsid) { struct nvme_ns *ns, *ret = NULL; @@ -3717,6 +3707,22 @@ struct nvme_ns *nvme_find_get_ns(struct nvme_ctrl *ctrl, unsigned nsid) } EXPORT_SYMBOL_NS_GPL(nvme_find_get_ns, NVME_TARGET_PASSTHRU); +/* + * Add the namespace to the controller list while keeping the list ordered. + */ +static void nvme_ns_add_to_ctrl_list(struct nvme_ns *ns) +{ + struct nvme_ns *tmp; + + list_for_each_entry_reverse(tmp, &ns->ctrl->namespaces, list) { + if (tmp->head->ns_id < ns->head->ns_id) { + list_add(&ns->list, &tmp->list); + return; + } + } + list_add(&ns->list, &ns->ctrl->namespaces); +} + static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid, struct nvme_ns_ids *ids) { @@ -3778,9 +3784,8 @@ static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid, } down_write(&ctrl->namespaces_rwsem); - list_add_tail(&ns->list, &ctrl->namespaces); + nvme_ns_add_to_ctrl_list(ns); up_write(&ctrl->namespaces_rwsem); - nvme_get_ctrl(ctrl); device_add_disk(ctrl->device, ns->disk, nvme_ns_id_attr_groups); @@ -4059,10 +4064,6 @@ static void nvme_scan_work(struct work_struct *work) if (nvme_scan_ns_list(ctrl) != 0) nvme_scan_ns_sequential(ctrl); mutex_unlock(&ctrl->scan_lock); - - down_write(&ctrl->namespaces_rwsem); - list_sort(NULL, &ctrl->namespaces, ns_cmp); - up_write(&ctrl->namespaces_rwsem); } /* -- 2.33.0