Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp247621rdh; Thu, 23 Nov 2023 03:01:14 -0800 (PST) X-Google-Smtp-Source: AGHT+IF8lEoxGMQzuOGTVD+Da6g/1WTgejb7IKymSiFDuzsRQf1BD6PZeg7bjQS0zpo8laZ21ruC X-Received: by 2002:a17:902:dad1:b0:1cf:56e1:ba98 with SMTP id q17-20020a170902dad100b001cf56e1ba98mr5196236plx.18.1700737274401; Thu, 23 Nov 2023 03:01:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700737274; cv=none; d=google.com; s=arc-20160816; b=y0IfC+UHoisXc38UgAkbTkQ0rU0RN0CXpJYk33I/zfE+trZtONWDlJCm6LK51ed3QZ VsnpXJwnaPW/CqBY1XuOdhTEAuuiEg2+UA2zhOKzoElZ/DagkYCqRuw0hnkh9ROChiZp s/BlZqGLdlb5ECAOakSGiFuCikvUdOps6zG2mWlvp2UrE0NRBAMjHM89eoOzZYgZeX4l EgadHd64EzADQqSW2OyDSvrOb0teh1F0kF4cvDoIwiyvilbQGzGQC29ieYfotmoUSaBI 4W4Zq360EwuYTPdAnB0Ld6kFQHFGRW8cJWv4Rr8Tbxs+5635yTOeKNeUx24KKwREAPNX Goxw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:message-id:date:subject:cc:to:from; bh=EW8cM780DyYloJn9iCXNpnTxx8FSx9qyfXJQnz3/b6Q=; fh=NdTmD4XCMwtfKoMJRV5z1g3jGcxPWzK7001bu6Ntn8g=; b=VrkOtpudLCxhD7en35Wzs3FmQnKSjhVtFiyXdkgsh1gotVqq1oiWmsYu8Bi629J07m EBr5a28ian1K7e4uQ/Vme8LZ2k2E3e/0YyKE9EV3zigFDhHxkhadIEtV83lL5cTrcpCu y+UN2AJG4jagM8rpAoxMX/6HAg5eU2DpM/6JqDxUtgzo7BkDJVoy/jnXWFjq6dqWDDZu jBsqzLDxO416Q/HehENywTJXDOv3qOqDikKrPMoCwU8bm80ElVXPbumXnuxjdtaoHSGs d8VNW2YqqN4exV0cwrGEv2O1AcJaSFALEpOTiRKCxPyX4mdAYcT7zY40+6eT0915Og0d ShFA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 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 fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id u10-20020a170902e5ca00b001cf65bade43si925444plf.542.2023.11.23.03.00.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Nov 2023 03:01:14 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 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 fry.vger.email (Postfix) with ESMTP id 077C1825A686; Thu, 23 Nov 2023 03:00:48 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344796AbjKWLAc (ORCPT + 99 others); Thu, 23 Nov 2023 06:00:32 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53720 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229542AbjKWLAa (ORCPT ); Thu, 23 Nov 2023 06:00:30 -0500 Received: from out30-119.freemail.mail.aliyun.com (out30-119.freemail.mail.aliyun.com [115.124.30.119]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4D0CF1BE for ; Thu, 23 Nov 2023 03:00:36 -0800 (PST) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R661e4;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_---0VwzFwnV_1700737214; Received: from localhost(mailfrom:yaoma@linux.alibaba.com fp:SMTPD_---0VwzFwnV_1700737214) by smtp.aliyun-inc.com; Thu, 23 Nov 2023 19:00:34 +0800 From: Bitao Hu To: kbusch@kernel.org, axboe@kernel.dk, hch@lst.de, sagi@grimberg.me Cc: linux-nvme@lists.infradead.org, linux-kernel@vger.kernel.org, kanie@linux.alibaba.com Subject: [PATCH] nvme: fix deadlock between reset and scan Date: Thu, 23 Nov 2023 19:00:13 +0800 Message-Id: <1700737213-110685-1-git-send-email-yaoma@linux.alibaba.com> X-Mailer: git-send-email 1.8.3.1 X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,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 fry.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 (fry.vger.email [0.0.0.0]); Thu, 23 Nov 2023 03:00:48 -0800 (PST) If controller reset occurs when allocating namespace, both nvme_reset_work and nvme_scan_work will hang, as shown below. Test Scripts: for ((t=1;t<=128;t++)) do nsid=`nvme create-ns /dev/nvme1 -s 14537724 -c 14537724 -f 0 -m 0 \ -d 0 | awk -F: '{print($NF);}'` nvme attach-ns /dev/nvme1 -n $nsid -c 0 done nvme reset /dev/nvme1 We will find that both nvme_reset_work and nvme_scan_work hung: INFO: task kworker/u249:4:17848 blocked for more than 120 seconds. "echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this message. task:kworker/u249:4 state:D stack: 0 pid:17848 ppid: 2 flags:0x00000028 Workqueue: nvme-reset-wq nvme_reset_work [nvme] Call trace: __switch_to+0xb4/0xfc __schedule+0x22c/0x670 schedule+0x4c/0xd0 blk_mq_freeze_queue_wait+0x84/0xc0 nvme_wait_freeze+0x40/0x64 [nvme_core] nvme_reset_work+0x1c0/0x5cc [nvme] process_one_work+0x1d8/0x4b0 worker_thread+0x230/0x440 kthread+0x114/0x120 INFO: task kworker/u249:3:22404 blocked for more than 120 seconds. "echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this message. task:kworker/u249:3 state:D stack: 0 pid:22404 ppid: 2 flags:0x00000028 Workqueue: nvme-wq nvme_scan_work [nvme_core] Call trace: __switch_to+0xb4/0xfc __schedule+0x22c/0x670 schedule+0x4c/0xd0 rwsem_down_write_slowpath+0x32c/0x98c down_write+0x70/0x80 nvme_alloc_ns+0x1ac/0x38c [nvme_core] nvme_validate_or_alloc_ns+0xbc/0x150 [nvme_core] nvme_scan_ns_list+0xe8/0x2e4 [nvme_core] nvme_scan_work+0x60/0x500 [nvme_core] process_one_work+0x1d8/0x4b0 worker_thread+0x260/0x440 kthread+0x114/0x120 INFO: task nvme:28428 blocked for more than 120 seconds. "echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this message. task:nvme state:D stack: 0 pid:28428 ppid: 27119 flags:0x00000000 Call trace: __switch_to+0xb4/0xfc __schedule+0x22c/0x670 schedule+0x4c/0xd0 schedule_timeout+0x160/0x194 do_wait_for_common+0xac/0x1d0 __wait_for_common+0x78/0x100 wait_for_completion+0x24/0x30 __flush_work.isra.0+0x74/0x90 flush_work+0x14/0x20 nvme_reset_ctrl_sync+0x50/0x74 [nvme_core] nvme_dev_ioctl+0x1b0/0x250 [nvme_core] __arm64_sys_ioctl+0xa8/0xf0 el0_svc_common+0x88/0x234 do_el0_svc+0x7c/0x90 el0_svc+0x1c/0x30 el0_sync_handler+0xa8/0xb0 el0_sync+0x148/0x180 The reason for the hang is that nvme_reset_work occurs while nvme_scan_work is still running. nvme_scan_work may add new ns into ctrl->namespaces list after nvme_reset_work frozen all ns->q in ctrl->namespaces list. The newly added ns is not frozen, so nvme_wait_freeze will wait forever. Unfortunately, ctrl->namespaces_rwsem is held by nvme_reset_work, so nvme_scan_work will also wait forever. Now we are deadlocked! PROCESS1 PROCESS2 ============== ============== nvme_scan_work ... nvme_reset_work nvme_validate_or_alloc_ns nvme_dev_disable nvme_alloc_ns nvme_start_freeze down_write ... nvme_ns_add_to_ctrl_list ... up_write nvme_wait_freeze ... down_read nvme_alloc_ns blk_mq_freeze_queue_wait down_write Fix by checking ctrl->state whether is NVME_CTRL_LIVE before adding new ns into ctrl->namespaces. Signed-off-by: Bitao Hu --- drivers/nvme/host/core.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/drivers/nvme/host/core.c b/drivers/nvme/host/core.c index 62612f8..7551b55 100644 --- a/drivers/nvme/host/core.c +++ b/drivers/nvme/host/core.c @@ -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)) { + 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); -- 1.8.3.1