Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 03F0AC433FE for ; Wed, 5 Oct 2022 14:20:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:In-Reply-To:Content-Type: MIME-Version:References:Message-ID:Subject:Cc:To:From:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=/XXZ2qscynTLcIoyJYXPqnjyY+u3XLRJKK9KRZBvGnM=; b=Y+3q/WJa3Du4rk4ruHZBnZfaZE /m9Zb0m1KwC1VtlUT1BHlqzkWDOCusv+mIQ4QPakHcNW7PuQvjxQJgCU3qfXlH27Y8IXfepZnSaZ5 vh7odgJMG6b5sKRJVxqS7tkqMzJjx/YUJV88A/6ZagcJzI5wzL4A4KnhPp3RZ+esZNPTdLumS5TIp hYulZPjCFVcdez+OQAEc5Zq9Uw0Nm+clzwPC6rZIOPxUnXomMSRhCIjZ9aJkTGoVHYL6+6AFQb9h/ aHfdMpi7IXvtKe0fTuPSgZ0Z0L4BrhFKWlfJLFjzFy8zgcQS9GHciue6pz6N/6VuF9Vjb+wGhZq9l 2TFT1/Ig==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1og5Fo-00EUTT-Lg; Wed, 05 Oct 2022 14:20:08 +0000 Received: from dfw.source.kernel.org ([139.178.84.217]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1og5Fl-00EUSx-AI for linux-nvme@lists.infradead.org; Wed, 05 Oct 2022 14:20:06 +0000 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id BF46C61704; Wed, 5 Oct 2022 14:20:04 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9E7A4C433C1; Wed, 5 Oct 2022 14:20:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1664979604; bh=J4LvHXg4Q3IG9Xuu/C8ZiHp05LrKnIyPnVCmA9dmmLo=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=nq6BwKm7dsAnHwKS0KOcq5hI9a+QPU1DN87g67dmrnywX8vVfUQe+6PNa7bLOrPgJ Fd7DDJZ345nRp12nKsuJEJxx2dv/QF7RhspOvUoCC+dwEy0ejZNW5/92RBDEk/hsHC QNiLThFm9V2m/xgZ9HJc/spiLmXCtokCDQCSGq9Bc/6/2k1ZT6zVmjAjWr8CflyN/x hVsZegRFDPBDXhoTVA7+7IAKBYYGKC8Dy1jpq6JBl/sLkYskgnBKyhZnJmcNWem51N ytSQrkpM1I/8yLZBTVsaBOfUedt2+nGSRr4bb3kksiK/HPD3Hu2u8OirftCOsB5+bv rxv1JZG1kUWzg== Date: Wed, 5 Oct 2022 08:20:00 -0600 From: Keith Busch To: Tetsuo Handa Cc: Shinichiro Kawasaki , "linux-block@vger.kernel.org" , "linux-nvme@lists.infradead.org" , Tejun Heo , Johannes Thumshirn , Damien Le Moal Subject: Re: lockdep WARNING at blktests block/011 Message-ID: References: <20220930001943.zdbvolc3gkekfmcv@shindev> <313d914e-6258-50db-4317-0ffb6f936553@I-love.SAKURA.ne.jp> <20221003133240.bq2vynauksivj55x@shindev> <20221004104456.ik42oxynyujsu5vb@shindev> <63e14e00-e877-cb5a-a69a-ff44bed8b5a5@I-love.SAKURA.ne.jp> <20221004122354.xxqpughpvnisz5qs@shindev> <20221005083104.2k7nqohqcqcrdpn4@shindev> <15c6e51f-a2a4-38ff-15a4-9efee32824d3@I-love.SAKURA.ne.jp> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <15c6e51f-a2a4-38ff-15a4-9efee32824d3@I-love.SAKURA.ne.jp> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20221005_072005_457026_44658982 X-CRM114-Status: GOOD ( 24.45 ) X-BeenThere: linux-nvme@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "Linux-nvme" Errors-To: linux-nvme-bounces+linux-nvme=archiver.kernel.org@lists.infradead.org On Wed, Oct 05, 2022 at 07:00:30PM +0900, Tetsuo Handa wrote: > On 2022/10/05 17:31, Shinichiro Kawasaki wrote: > > @@ -5120,11 +5120,27 @@ EXPORT_SYMBOL_GPL(nvme_start_admin_queue); > > void nvme_sync_io_queues(struct nvme_ctrl *ctrl) > > { > > struct nvme_ns *ns; > > + LIST_HEAD(splice); > > > > - down_read(&ctrl->namespaces_rwsem); > > - list_for_each_entry(ns, &ctrl->namespaces, list) > > + /* > > + * blk_sync_queues() call in ctrl->snamespaces_rwsem critical section > > + * triggers deadlock warning by lockdep since cancel_work_sync() in > > + * blk_sync_queue() waits for nvme_timeout() work completion which may > > + * lock the ctrl->snamespaces_rwsem. To avoid the deadlock possibility, > > + * call blk_sync_queues() out of the critical section by moving the > > + * ctrl->namespaces list elements to the stack list head temporally. > > + */ > > + > > + down_write(&ctrl->namespaces_rwsem); > > + list_splice_init(&ctrl->namespaces, &splice); > > + up_write(&ctrl->namespaces_rwsem); > > Does this work? > > ctrl->namespaces being empty when calling blk_sync_queue() means that > e.g. nvme_start_freeze() cannot find namespaces to freeze, doesn't it? There can't be anything to timeout at this point. The controller is disabled prior to syncing the queues. Not only is there no IO for timeout work to operate on, the controller state is already disabled, so a subsequent freeze would be skipped. > blk_mq_timeout_work(work) { // Is blocking __flush_work() from cancel_work_sync(). > blk_mq_queue_tag_busy_iter(blk_mq_check_expired) { > bt_for_each(blk_mq_check_expired) == blk_mq_check_expired() { > blk_mq_rq_timed_out() { > req->q->mq_ops->timeout(req) == nvme_timeout(req) { > nvme_dev_disable() { > mutex_lock(&dev->shutdown_lock); // Holds dev->shutdown_lock > nvme_start_freeze(&dev->ctrl) { > down_read(&ctrl->namespaces_rwsem); // Holds ctrl->namespaces_rwsem which might block > //blk_freeze_queue_start(ns->queue); // <= Never be called because ctrl->namespaces is empty. > up_read(&ctrl->namespaces_rwsem); > } > mutex_unlock(&dev->shutdown_lock); > } > } > } > } > } > } > > Are you sure that down_read(&ctrl->namespaces_rwsem) users won't run > when ctrl->namespaces is temporarily made empty? (And if you are sure > that down_read(&ctrl->namespaces_rwsem) users won't run when > ctrl->namespaces is temporarily made empty, why ctrl->namespaces_rwsem > needs to be a rw-sem rather than a plain mutex or spinlock ?) We iterate the list in some target fast paths, so we don't want this to be an exclusive section for readers. > > + list_for_each_entry(ns, &splice, list) > > blk_sync_queue(ns->queue); > > - up_read(&ctrl->namespaces_rwsem); > > + > > + down_write(&ctrl->namespaces_rwsem); > > + list_splice(&splice, &ctrl->namespaces); > > + up_write(&ctrl->namespaces_rwsem); > > } > > EXPORT_SYMBOL_GPL(nvme_sync_io_queues); > > I don't know about dependency chain, but you might be able to add > "struct nvme_ctrl"->sync_io_queue_mutex which is held for serializing > nvme_sync_io_queues() and down_write(&ctrl->namespaces_rwsem) users? > > If we can guarantee that ctrl->namespaces_rwsem => ctrl->sync_io_queue_mutex > is impossible, nvme_sync_io_queues() can use ctrl->sync_io_queue_mutex > rather than ctrl->namespaces_rwsem, and down_write(&ctrl->namespaces_rwsem)/ > up_write(&ctrl->namespaces_rwsem) users are replaced with > mutex_lock(&ctrl->sync_io_queue_mutex); > down_write(&ctrl->namespaces_rwsem); > and > up_write(&ctrl->namespaces_rwsem); > mutex_unlock(&ctrl->sync_io_queue_mutex); > sequences respectively. >