Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp3638511rdh; Mon, 27 Nov 2023 22:22:52 -0800 (PST) X-Google-Smtp-Source: AGHT+IG13ylgWP+wJEHqbk/VQZ+XegHiyLNRKL/TXI2oaE7WBe8UbGWjHajE172i8VpHy93RMN+1 X-Received: by 2002:a05:6870:168e:b0:1fa:171c:9e27 with SMTP id j14-20020a056870168e00b001fa171c9e27mr15502488oae.0.1701152572172; Mon, 27 Nov 2023 22:22:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701152572; cv=none; d=google.com; s=arc-20160816; b=1JndOWleGWjInqBtO5N/e19knu5Z1sXFJ1S7zaQ0k13NH4aa/CRa5BRBuCHQV7rfeJ 4BVFtTAWeLKfZJaJ4FDdSnIyDw66jgaBXlKLNDRAtLeesCpDyPR3hRA8JyS0fYbXpbqE jLGT7sKzPpVy/jeKpdHtdciBY7Furl6tBFpmw9rHjeafT0hkQ6yhaqtId5NCFD2eQlDv R9rMBwN7prmZSvzg4eE8Bvh8JgoIg262/RaiJ2pg16c8FdSfyF7nx/lz4vpivPoolWOG P4YosPgZaDCVkkyVphQzdFvXKzH2lkAObGQwbjUINUupU8qg8QA8Me+oaasY/6/+3f0t NIvQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id; bh=85td8lfeKN6qjBLmZIKTnsiFfWCuq1Xuhwm1YzEO3Qs=; fh=4TA/TPLBZeU1JSK45cxnPvCwfmkm1b6Ezx4amz0UHuM=; b=GEDckZ3BBQWvJ5j7uwszBKopldgyTHo5zXFZVrVjSvvsNUgR1LwtDCLBsZs+nNqIzL bSpyof6ehrwqEGYTAq3Qs9tyGo/31svekPN6+JaAm0RHPNHkTuvN0ws6jaWiqrIWa90S IY4x+KpKT6/QfEATikX42mAlTjXiehOan5hPBQncPl9OiACGhflGWgLU/YsRqx5TRXnX 7OQ7ZRcHFM207Fl8x3Q+eRYew5xzzO80EiGCb52M7C/D2dQvdEcrc2CB3QKjh0oDhO/g DI3MemXokYdYTd4RuTHYFd9gUaOHrw9vV4j8SlLeDiXStprs+fjlwCaVAoY+LOLqqngd CktQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Return-Path: Received: from pete.vger.email (pete.vger.email. [23.128.96.36]) by mx.google.com with ESMTPS id dc14-20020a056a0035ce00b006cbfbb564edsi8643134pfb.129.2023.11.27.22.22.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Nov 2023 22:22:52 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) client-ip=23.128.96.36; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 770208199715; Mon, 27 Nov 2023 22:22:49 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343576AbjK1GWa (ORCPT + 99 others); Tue, 28 Nov 2023 01:22:30 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33216 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232615AbjK1GW3 (ORCPT ); Tue, 28 Nov 2023 01:22:29 -0500 Received: from out30-101.freemail.mail.aliyun.com (out30-101.freemail.mail.aliyun.com [115.124.30.101]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6BC9E183 for ; Mon, 27 Nov 2023 22:22:35 -0800 (PST) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R101e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018046050;MF=yaoma@linux.alibaba.com;NM=1;PH=DS;RN=7;SR=0;TI=SMTPD_---0VxJbmTM_1701152551; Received: from 30.178.66.233(mailfrom:yaoma@linux.alibaba.com fp:SMTPD_---0VxJbmTM_1701152551) by smtp.aliyun-inc.com; Tue, 28 Nov 2023 14:22:33 +0800 Message-ID: <65b0c372-b308-46dd-c2f2-a5ddb50adb10@linux.alibaba.com> Date: Tue, 28 Nov 2023 14:22:27 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.15.1 Subject: Re: [PATCH] nvme: fix deadlock between reset and scan Content-Language: en-US To: Keith Busch Cc: axboe@kernel.dk, hch@lst.de, sagi@grimberg.me, linux-nvme@lists.infradead.org, linux-kernel@vger.kernel.org, kanie@linux.alibaba.com References: <1700737213-110685-1-git-send-email-yaoma@linux.alibaba.com> From: yaoma In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-2.6 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,UNPARSEABLE_RELAY autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Mon, 27 Nov 2023 22:22:49 -0800 (PST) Hi Keith Busch Thanks for your reply. The idea to avoid such a deadlock between nvme_reset and nvme_scan is to ensure that no namespace can be added to ctrl->namespaces after nvme_start_freeze has already been called. We can achieve this goal by assessing the ctrl->state after we have already acquired the ctrl->namespaces_rwsem lock, to decide whether to add the namespace to the list or not. 1. After we determine that ctrl->state is LIVE, it may be immediately changed to another state. However, since we have already acquired the lock, other tasks cannot access ctrl->namespace, so we can still safely add the namespace to the list. After acquiring the lock, nvme_start_freeze will freeze all ns->q in the list, including any newly added namespaces. 2. Before the completion of nvme_reset, ctrl->state will not be changed to LIVE, so we will not add any more namespaces to the list. All ns->q in the list is frozen, so nvme_wait_freeze can exit normally. On 2023/11/28 02:07, Keith Busch wrote: > On Thu, Nov 23, 2023 at 07:00:13PM +0800, Bitao Hu wrote: >> @@ -3631,6 +3631,11 @@ static void nvme_alloc_ns(struct nvme_ctrl *ctrl, struct nvme_ns_info *info) >> goto out_unlink_ns; >> >> down_write(&ctrl->namespaces_rwsem); >> + /* preventing adding ns during resetting */ >> + if (unlikely(ctrl->state != NVME_CTRL_LIVE)) { > > We can't rely on ctrl->state for preventing deadlocks. Reading unlocked > ctrl->state is often used, but should be considered advisory-only since > the state could change immediatly after reading it. > >> + up_write(&ctrl->namespaces_rwsem); >> + goto out_unlink_ns; >> + } >> nvme_ns_add_to_ctrl_list(ns); >> up_write(&ctrl->namespaces_rwsem); >> nvme_get_ctrl(ctrl);