Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp919835pxb; Wed, 1 Sep 2021 12:41:42 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyvfeUCKZ2nob72zpzIwxOU4eRdrxPUYLTP2dqolnmjtbPZfDw74kyrS9WjWmXAcwWiu4Rx X-Received: by 2002:a05:6402:5215:: with SMTP id s21mr1268455edd.236.1630525301882; Wed, 01 Sep 2021 12:41:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1630525301; cv=none; d=google.com; s=arc-20160816; b=bMYfyegruAHR2e6/S/O6RRXFNnIN+iQVkQT7D9fRxzC/j3TXOvnaQHU0nUM5D8iIDK zNFJcx5cWOEEn5RKLgrsP1u2hlVgY+XKZDb9FF40wByv9xZwgUcw3taT/7i1M4T3qEAJ ZziEwnSvluTTcQ3sek3MonXkwocwcCPfVUWSes7931N/+j+ttZYLzvKfxqq9qzKm7tm9 Difa3/HgMsie9NLBfWZSH42LEtVf31qaRaSZYJdPsBLgksHXiq37OTaZ1YJSWJCHMe8J g3kAOgvrmimZwHSo7JzxqoQU+cBLfMhZ0Z9PAs0/tjYEd7Tpa5XWw6ySHDkp0X3jzwi1 kqoA== 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=GOPvK3rUWHncfXL8wuaTVvn3oX2fr9pCEhIDbgpSzIU=; b=sLtjhzoLDaX4k+qukm+JTA4Q6rZCz0VCAFwLNThj3UqcAAcmTkoezNivA11o+JknMb SKWAF/jA9FO7oXfV4ywpZWU7SnXD0cdtXdrSeeiS9OKLhYt/Ipvv/htkVPNsy/1FgbgU o5JQz6EkN1PNSkUB/KZPAiFvlXCKph0F2cOd51kkmicOELSGCv3Vh4N7S8U3NdjMhKPM jZlI2NkBY1WvfdQ+BHlW4U+5IMjZAGRqJE6VoEs2sF1EEeUvZGWi3x8ObttKXs/UbjWS gyX4CSh0H/12HUKouP7KRJxwX9Sw/yjoLZGYRGrADTO7yMqVGX3NiNvKdI6IzJAgKJl3 UASA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=aqKhioCi; 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 i29si732509ejo.398.2021.09.01.12.40.59; Wed, 01 Sep 2021 12:41:41 -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=aqKhioCi; 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 S1345317AbhIAMrg (ORCPT + 99 others); Wed, 1 Sep 2021 08:47:36 -0400 Received: from mail.kernel.org ([198.145.29.99]:47722 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345457AbhIAMoQ (ORCPT ); Wed, 1 Sep 2021 08:44:16 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 3EA9C610C9; Wed, 1 Sep 2021 12:38:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1630499917; bh=uFoGyHp9snzNt123frTbdjiDX9s2ycIlpFAozepiU4g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=aqKhioCi+sUVrsLk4qVOr9cVbg4CbH9DaPmDRVv8QwoRSrfAQgRJysJfQhsQfGZIt 2aVef5liqHEUPyd+HuStgJtJ0aemXFrra7nhHubcgsssPt+HRTdbyUoUfJ6sbSujWG CZ9OyPw3tWn2KKNzVUKaTVSLewCr3S5LyDfpLvaw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Bart Van Assche , Li Jinlin , Qiu Laibin , "Martin K. Petersen" Subject: [PATCH 5.13 034/113] scsi: core: Fix hang of freezing queue between blocking and running device Date: Wed, 1 Sep 2021 14:27:49 +0200 Message-Id: <20210901122303.130577318@linuxfoundation.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210901122301.984263453@linuxfoundation.org> References: <20210901122301.984263453@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: Li Jinlin commit 02c6dcd543f8f051973ee18bfbc4dc3bd595c558 upstream. We found a hang, the steps to reproduce are as follows: 1. blocking device via scsi_device_set_state() 2. dd if=/dev/sda of=/mnt/t.log bs=1M count=10 3. echo none > /sys/block/sda/queue/scheduler 4. echo "running" >/sys/block/sda/device/state Step 3 and 4 should complete after step 4, but they hang. CPU#0 CPU#1 CPU#2 --------------- ---------------- ---------------- Step 1: blocking device Step 2: dd xxxx ^^^^^^ get request q_usage_counter++ Step 3: switching scheculer elv_iosched_store elevator_switch blk_mq_freeze_queue blk_freeze_queue > blk_freeze_queue_start ^^^^^^ mq_freeze_depth++ > blk_mq_run_hw_queues ^^^^^^ can't run queue when dev blocked > blk_mq_freeze_queue_wait ^^^^^^ Hang here!!! wait q_usage_counter==0 Step 4: running device store_state_field scsi_rescan_device scsi_attach_vpd scsi_vpd_inquiry __scsi_execute blk_get_request blk_mq_alloc_request blk_queue_enter ^^^^^^ Hang here!!! wait mq_freeze_depth==0 blk_mq_run_hw_queues ^^^^^^ dispatch IO, q_usage_counter will reduce to zero blk_mq_unfreeze_queue ^^^^^ mq_freeze_depth-- To fix this, we need to run queue before rescanning device when the device state changes to SDEV_RUNNING. Link: https://lore.kernel.org/r/20210824025921.3277629-1-lijinlin3@huawei.com Fixes: f0f82e2476f6 ("scsi: core: Fix capacity set to zero after offlinining device") Reviewed-by: Bart Van Assche Signed-off-by: Li Jinlin Signed-off-by: Qiu Laibin Signed-off-by: Martin K. Petersen Signed-off-by: Greg Kroah-Hartman --- drivers/scsi/scsi_sysfs.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) --- a/drivers/scsi/scsi_sysfs.c +++ b/drivers/scsi/scsi_sysfs.c @@ -808,12 +808,15 @@ store_state_field(struct device *dev, st ret = scsi_device_set_state(sdev, state); /* * If the device state changes to SDEV_RUNNING, we need to - * rescan the device to revalidate it, and run the queue to - * avoid I/O hang. + * run the queue to avoid I/O hang, and rescan the device + * to revalidate it. Running the queue first is necessary + * because another thread may be waiting inside + * blk_mq_freeze_queue_wait() and because that call may be + * waiting for pending I/O to finish. */ if (ret == 0 && state == SDEV_RUNNING) { - scsi_rescan_device(dev); blk_mq_run_hw_queues(sdev->request_queue, true); + scsi_rescan_device(dev); } mutex_unlock(&sdev->state_mutex);