Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp2539394ybb; Mon, 30 Mar 2020 08:07:10 -0700 (PDT) X-Google-Smtp-Source: ADFU+vshottL8h/mcLHGv28yBARr43VZVWr6/vrvQL82ZFyd8PSUUWpqHF11A4uQ4w/gE1htxgI0 X-Received: by 2002:aca:cd0e:: with SMTP id d14mr7267893oig.167.1585580829651; Mon, 30 Mar 2020 08:07:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1585580829; cv=none; d=google.com; s=arc-20160816; b=zxHhpqnk4VrOwiELWEuPiLP+W1+83X4vEF4WZVmObX1j2WM91HUm2/R+Y+g9TJewn9 7rPmEUZ8+Vric9/jD6Hz35D1/eGqUpOnPiMDwvd1lY7345Kbe5dh2aEQSaM01CEmThfW HKcMdMtOIp4qDV1LxDN8tsCliZlcMcvghAFzvODpDulrfEIdnb8Tqqebee0+SMnXd6Af DNNT3iXZSKRZl3bd/Kn/Xkplf7VDoZ4KTHCNZLITZtUOhuW/8CYD2Sj3AkvtvF4J16kb mBfJlVAcN+0T/ecI+RYh6HDj4wuTnkw2B6ptwaQEv0I/6xJM3S+54sh3joBDlUTC8ovF 2oWw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=dM88C1QGqBI6GelWDoTaSxU6lxBt7GdfPhOw1+SN7L4=; b=MxAG2mo5g0jIGUfHhb5ujD7xic3uToyQBkFUHWjZT2PbbPGnVkbyO9LEHtpoWDJhJn VltDcsTiTxIyMQQixEPwWZ+YGbNMCt76HNTKi3tN9z+AhjfjYLJrbUzvWERblv3AbZf+ Xt1CUiZy8od7XZByOv2UJqx4lZOvemL3n91IB4iFqFto3s5GdZFRFFWlJzvL8cBAs6L2 HGxsBYvAeuVKoJYZ00f0fxSsGNZdW3bS+zjoansqIXc02gBAzsrlFpXXxm7HRcogsJpw sZnuH2QWcoBfdIwVfkFVKM/4aZScmC/s+jqj/xx7Z/CGr62/Qartv5HNWMC1ZeitHYMA n3IQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=bZQT8U7y; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h4si5819063oie.125.2020.03.30.08.06.51; Mon, 30 Mar 2020 08:07:09 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=bZQT8U7y; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728716AbgC3Otx (ORCPT + 99 others); Mon, 30 Mar 2020 10:49:53 -0400 Received: from mail-pl1-f195.google.com ([209.85.214.195]:45837 "EHLO mail-pl1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728658AbgC3Otv (ORCPT ); Mon, 30 Mar 2020 10:49:51 -0400 Received: by mail-pl1-f195.google.com with SMTP id t4so854155plq.12 for ; Mon, 30 Mar 2020 07:49:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=dM88C1QGqBI6GelWDoTaSxU6lxBt7GdfPhOw1+SN7L4=; b=bZQT8U7yeFC0d8TYNbHn5nxihcCGYzsucQuteR7S/owSBM74VPEGKfvp1QygPlLklu 7A8Cf+UJ2EJnU6xm+0oGKWQydb4lR8JoJNOjkbL9XKP7aFLsL40p+BMaSrDFU1lQNio9 7f/kbrNNjQ/MHbdaUvWE0x8rRGWFOqb6aL9sA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=dM88C1QGqBI6GelWDoTaSxU6lxBt7GdfPhOw1+SN7L4=; b=k6aDJinYhB/BQVutwlaujams64JncitePG6rPU5t63OwyxXoK/53lExgS4FdOS45Ma RP7FjlJgD/WKpCRnDPtpSdcFeUy7zTUCSbMzsMgTI3we+gv2jtHjkwbAPGQXVrWed+lS 1InnaIwBqFrfQnNoExPkuzyoLd8IOgyTk7Q6YhJPnduZ7nK55FSrdKDE7eWbha159iLd l/xMgKpKXq4Ayopq1kUqYdcpS9/IKh4KSwOQwH1GUi6At2DaHU3cXnkaSnAsYuk6e5+E py1Zqrk90X9g/Pab1xedroC9uxzxn4sqvAP2w3MPlTrcgCG1YoJXMf4YZQLLPX5a+dMk yQhg== X-Gm-Message-State: ANhLgQ23fLRxKvidueh7dHdQ35NzA1HNTs72mT9ZDWV9sha80DEPoirz qIPKcTIChQ1qD8mHipLZpq3TmQ== X-Received: by 2002:a17:90a:7f03:: with SMTP id k3mr16139676pjl.42.1585579788982; Mon, 30 Mar 2020 07:49:48 -0700 (PDT) Received: from tictac2.mtv.corp.google.com ([2620:15c:202:1:24fa:e766:52c9:e3b2]) by smtp.gmail.com with ESMTPSA id y198sm1460972pfg.123.2020.03.30.07.49.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Mar 2020 07:49:48 -0700 (PDT) From: Douglas Anderson To: axboe@kernel.dk, jejb@linux.ibm.com, martin.petersen@oracle.com Cc: linux-block@vger.kernel.org, groeck@chromium.org, paolo.valente@linaro.org, linux-scsi@vger.kernel.org, sqazi@google.com, Douglas Anderson , linux-kernel@vger.kernel.org Subject: [PATCH 2/2] scsi: core: Fix stall if two threads request budget at the same time Date: Mon, 30 Mar 2020 07:49:06 -0700 Message-Id: <20200330074856.2.I28278ef8ea27afc0ec7e597752a6d4e58c16176f@changeid> X-Mailer: git-send-email 2.26.0.rc2.310.g2932bb562d-goog In-Reply-To: <20200330144907.13011-1-dianders@chromium.org> References: <20200330144907.13011-1-dianders@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org It is possible for two threads to be running blk_mq_do_dispatch_sched() at the same time with the same "hctx". This is because there can be more than one caller to __blk_mq_run_hw_queue() with the same "hctx" and hctx_lock() doesn't prevent more than one thread from entering. If more than one thread is running blk_mq_do_dispatch_sched() at the same time with the same "hctx", they may have contention acquiring budget. The blk_mq_get_dispatch_budget() can eventually translate into scsi_mq_get_budget(). If the device's "queue_depth" is 1 (not uncommon) then only one of the two threads will be the one to increment "device_busy" to 1 and get the budget. The losing thread will break out of blk_mq_do_dispatch_sched() and will stop dispatching requests. The assumption is that when more budget is available later (when existing transactions finish) the queue will be kicked again, perhaps in scsi_end_request(). The winning thread now has budget and can go on to call dispatch_request(). If dispatch_request() returns NULL here then we have a potential problem. Specifically we'll now call blk_mq_put_dispatch_budget() which translates into scsi_mq_put_budget(). That will mark the device as no longer busy but doesn't do anything to kick the queue. This violates the assumption that the queue would be kicked when more budget was available. Pictorially: Thread A Thread B ================================= ================================== blk_mq_get_dispatch_budget() => 1 dispatch_request() => NULL blk_mq_get_dispatch_budget() => 0 // because Thread A marked // "device_busy" in scsi_device blk_mq_put_dispatch_budget() The above case was observed in reboot tests and caused a task to hang forever waiting for IO to complete. Traces showed that in fact two tasks were running blk_mq_do_dispatch_sched() at the same time with the same "hctx". The task that got the budget did in fact see dispatch_request() return NULL. Both tasks returned and the system went on for several minutes (until the hung task delay kicked in) without the given "hctx" showing up again in traces. Let's attempt to fix this problem by detecting budget contention. If we're in the SCSI code's put_budget() function and we saw that someone else might have wanted the budget we got then we'll kick the queue. The mechanism of kicking due to budget contention has the potential to overcompensate and kick the queue more than strictly necessary, but it shouldn't hurt. Signed-off-by: Douglas Anderson --- drivers/scsi/scsi_lib.c | 27 ++++++++++++++++++++++++--- drivers/scsi/scsi_scan.c | 1 + include/scsi/scsi_device.h | 2 ++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/drivers/scsi/scsi_lib.c b/drivers/scsi/scsi_lib.c index 610ee41fa54c..0530da909995 100644 --- a/drivers/scsi/scsi_lib.c +++ b/drivers/scsi/scsi_lib.c @@ -344,6 +344,21 @@ static void scsi_dec_host_busy(struct Scsi_Host *shost, struct scsi_cmnd *cmd) rcu_read_unlock(); } +static void scsi_device_dec_busy(struct scsi_device *sdev) +{ + bool was_contention; + unsigned long flags; + + spin_lock_irqsave(&sdev->budget_lock, flags); + atomic_dec(&sdev->device_busy); + was_contention = sdev->budget_contention; + sdev->budget_contention = false; + spin_unlock_irqrestore(&sdev->budget_lock, flags); + + if (was_contention) + blk_mq_run_hw_queues(sdev->request_queue, true); +} + void scsi_device_unbusy(struct scsi_device *sdev, struct scsi_cmnd *cmd) { struct Scsi_Host *shost = sdev->host; @@ -354,7 +369,7 @@ void scsi_device_unbusy(struct scsi_device *sdev, struct scsi_cmnd *cmd) if (starget->can_queue > 0) atomic_dec(&starget->target_busy); - atomic_dec(&sdev->device_busy); + scsi_device_dec_busy(sdev); } static void scsi_kick_queue(struct request_queue *q) @@ -1624,16 +1639,22 @@ static void scsi_mq_put_budget(struct blk_mq_hw_ctx *hctx) struct request_queue *q = hctx->queue; struct scsi_device *sdev = q->queuedata; - atomic_dec(&sdev->device_busy); + scsi_device_dec_busy(sdev); } static bool scsi_mq_get_budget(struct blk_mq_hw_ctx *hctx) { struct request_queue *q = hctx->queue; struct scsi_device *sdev = q->queuedata; + unsigned long flags; - if (scsi_dev_queue_ready(q, sdev)) + spin_lock_irqsave(&sdev->budget_lock, flags); + if (scsi_dev_queue_ready(q, sdev)) { + spin_unlock_irqrestore(&sdev->budget_lock, flags); return true; + } + sdev->budget_contention = true; + spin_unlock_irqrestore(&sdev->budget_lock, flags); if (atomic_read(&sdev->device_busy) == 0 && !scsi_device_blocked(sdev)) blk_mq_delay_run_hw_queue(hctx, SCSI_QUEUE_DELAY); diff --git a/drivers/scsi/scsi_scan.c b/drivers/scsi/scsi_scan.c index 058079f915f1..72f7b6faed9b 100644 --- a/drivers/scsi/scsi_scan.c +++ b/drivers/scsi/scsi_scan.c @@ -240,6 +240,7 @@ static struct scsi_device *scsi_alloc_sdev(struct scsi_target *starget, INIT_LIST_HEAD(&sdev->starved_entry); INIT_LIST_HEAD(&sdev->event_list); spin_lock_init(&sdev->list_lock); + spin_lock_init(&sdev->budget_lock); mutex_init(&sdev->inquiry_mutex); INIT_WORK(&sdev->event_work, scsi_evt_thread); INIT_WORK(&sdev->requeue_work, scsi_requeue_run_queue); diff --git a/include/scsi/scsi_device.h b/include/scsi/scsi_device.h index f8312a3e5b42..3c5e0f0c8a91 100644 --- a/include/scsi/scsi_device.h +++ b/include/scsi/scsi_device.h @@ -106,6 +106,8 @@ struct scsi_device { struct list_head siblings; /* list of all devices on this host */ struct list_head same_target_siblings; /* just the devices sharing same target id */ + spinlock_t budget_lock; /* For device_busy and budget_contention */ + bool budget_contention; /* Someone couldn't get budget */ atomic_t device_busy; /* commands actually active on LLDD */ atomic_t device_blocked; /* Device returned QUEUE_FULL. */ -- 2.26.0.rc2.310.g2932bb562d-goog