From patchwork Tue Apr 7 22:00:02 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Doug Anderson X-Patchwork-Id: 11479029 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 844541744 for ; Tue, 7 Apr 2020 22:00:57 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 62AB82074F for ; Tue, 7 Apr 2020 22:00:57 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="GmjVUTgA" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726525AbgDGWAl (ORCPT ); Tue, 7 Apr 2020 18:00:41 -0400 Received: from mail-pl1-f193.google.com ([209.85.214.193]:38826 "EHLO mail-pl1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726492AbgDGWAj (ORCPT ); Tue, 7 Apr 2020 18:00:39 -0400 Received: by mail-pl1-f193.google.com with SMTP id w3so1750191plz.5 for ; Tue, 07 Apr 2020 15:00:39 -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=xnXKID66OSSyAQqnf06AsglExR2cwtRMWmnbx6zKoBI=; b=GmjVUTgAUpx/envjSX4TqNkVRaWSyHW2WFhyprFYC5I5aR1kZ5yGL8K1fnGq/jh/Id 4xWWmL+Mmqjn1uPlwRxXiFVRSzGWbCUSfwvgzbYeN8i8+KCSKqrA1oOh/0dzyVkAYh2A hktk/Ygzn8DBXqV33ZNi7oub2uus1hiG3AiAg= 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=xnXKID66OSSyAQqnf06AsglExR2cwtRMWmnbx6zKoBI=; b=lY1lDyCx+/wXClRGCSsINRD+hUj5jFdqY1+Ls7au5U85eO++nZgykmedObAx8PSp0x NhWVe2DUUtmSbzR6z5ethXT+OgG5qkUUyffxpZ/q75jFtIv7mmwAIcqqkmoZuGlelDF8 HmD8ynA48LCpCgiNBRk/7QmEXLzoaKCBOuoWM4YeAgdG5gALTjn/hxk9Tf5J6L6MawOj 6fy78RaishIC1/3qVGIN8JZjo8U/c2obnCPIvh3eIpuD7D1ANvnCFyv8MFViDobSOia5 2y3QCRVMeZ7twzKuwu14xahqsfsZqAMQesdMZUZpRbu6gHE4uBRlj378giNLT+WEla1V hH2Q== X-Gm-Message-State: AGi0PuawdoBGIPzzJpIfKJYWDFZwEWKJr83uaqgdA0vixFS3pZhtyTdO pjKsQmGj9ArT4ZW9ZcNVjuMl0hxZs2uLUQ== X-Google-Smtp-Source: APiQypL5GY9tYiHTpTME8h8EpgisIlAdVi6iaupCxQLgo4LGuBLE09GyMgLM9vTrQYpw/ELJ5nXpgQ== X-Received: by 2002:a17:902:7682:: with SMTP id m2mr4194211pll.311.1586296839137; Tue, 07 Apr 2020 15:00:39 -0700 (PDT) Received: from tictac2.mtv.corp.google.com ([2620:15c:202:1:24fa:e766:52c9:e3b2]) by smtp.gmail.com with ESMTPSA id g3sm880112pgd.64.2020.04.07.15.00.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Apr 2020 15:00:38 -0700 (PDT) From: Douglas Anderson To: axboe@kernel.dk, jejb@linux.ibm.com, martin.petersen@oracle.com Cc: Ming Lei , linux-scsi@vger.kernel.org, sqazi@google.com, Gwendal Grignou , groeck@chromium.org, linux-block@vger.kernel.org, paolo.valente@linaro.org, Douglas Anderson , linux-kernel@vger.kernel.org Subject: [PATCH v3 1/4] blk-mq: In blk_mq_dispatch_rq_list() "no budget" is a reason to kick Date: Tue, 7 Apr 2020 15:00:02 -0700 Message-Id: <20200407145906.v3.1.I1f95c459e51962b8d2c83e869913b6befda2255c@changeid> X-Mailer: git-send-email 2.26.0.292.g33ef6b2f38-goog In-Reply-To: <20200407220005.119540-1-dianders@chromium.org> References: <20200407220005.119540-1-dianders@chromium.org> MIME-Version: 1.0 Sender: linux-scsi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org In blk_mq_dispatch_rq_list(), if blk_mq_sched_needs_restart() returns true and the driver returns BLK_STS_RESOURCE then we'll kick the queue. However, there's another case where we might need to kick it. If we were unable to get budget we can be in much the same state as when the driver returns BLK_STS_RESOURCE, so we should treat it the same. It should be noted that even if we add a whole bunch of extra kicking to the queue in other patches this patch is still important. Specifically any kicking that happened before we re-spliced leftover requests into 'hctx->dispatch' wouldn't have found any work, so we really need to make sure we kick ourselves after we've done the splicing. Signed-off-by: Douglas Anderson Reviewed-by: Ming Lei --- Changes in v3: - Note why blk_mq_dispatch_rq_list() change is needed. Changes in v2: None block/blk-mq.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/block/blk-mq.c b/block/blk-mq.c index f6291ceedee4..7d5f388b7da9 100644 --- a/block/blk-mq.c +++ b/block/blk-mq.c @@ -1206,6 +1206,7 @@ bool blk_mq_dispatch_rq_list(struct request_queue *q, struct list_head *list, bool no_tag = false; int errors, queued; blk_status_t ret = BLK_STS_OK; + bool no_budget_avail = false; if (list_empty(list)) return false; @@ -1222,8 +1223,10 @@ bool blk_mq_dispatch_rq_list(struct request_queue *q, struct list_head *list, rq = list_first_entry(list, struct request, queuelist); hctx = rq->mq_hctx; - if (!got_budget && !blk_mq_get_dispatch_budget(hctx)) + if (!got_budget && !blk_mq_get_dispatch_budget(hctx)) { + no_budget_avail = true; break; + } if (!blk_mq_get_driver_tag(rq)) { /* @@ -1318,13 +1321,15 @@ bool blk_mq_dispatch_rq_list(struct request_queue *q, struct list_head *list, * * If driver returns BLK_STS_RESOURCE and SCHED_RESTART * bit is set, run queue after a delay to avoid IO stalls - * that could otherwise occur if the queue is idle. + * that could otherwise occur if the queue is idle. We'll do + * similar if we couldn't get budget and SCHED_RESTART is set. */ needs_restart = blk_mq_sched_needs_restart(hctx); if (!needs_restart || (no_tag && list_empty_careful(&hctx->dispatch_wait.entry))) blk_mq_run_hw_queue(hctx, true); - else if (needs_restart && (ret == BLK_STS_RESOURCE)) + else if (needs_restart && (ret == BLK_STS_RESOURCE || + no_budget_avail)) blk_mq_delay_run_hw_queue(hctx, BLK_MQ_RESOURCE_DELAY); blk_mq_update_dispatch_busy(hctx, true); From patchwork Tue Apr 7 22:00:03 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Doug Anderson X-Patchwork-Id: 11479017 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 7E1951744 for ; Tue, 7 Apr 2020 22:00:44 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 5ABCA20753 for ; Tue, 7 Apr 2020 22:00:44 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="mRqdmR8z" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726541AbgDGWAm (ORCPT ); Tue, 7 Apr 2020 18:00:42 -0400 Received: from mail-pj1-f67.google.com ([209.85.216.67]:40824 "EHLO mail-pj1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726477AbgDGWAl (ORCPT ); Tue, 7 Apr 2020 18:00:41 -0400 Received: by mail-pj1-f67.google.com with SMTP id kx8so317766pjb.5 for ; Tue, 07 Apr 2020 15:00:40 -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=awDfB0MGcHAXl5KbIU74aBGmtjn2jxdDVI+x1Gk8xDs=; b=mRqdmR8zkn7Oe+uQ+gvTuRHtTrAik93NY0bijY+0jJIx/5WhlsXhcGiZu+mud4zJh/ vA3KnP3CnypjDiKg0Pt1RSaSrHd+g0rk3V8TGbWm50PtEiCKkbjh1ChsF6xFrL2gQJqt PpVKJ0PnKPe2te8xnLj0vPYnJUoSzX0X/jHtk= 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=awDfB0MGcHAXl5KbIU74aBGmtjn2jxdDVI+x1Gk8xDs=; b=q51oHvaCnlwmo4zx/o8bbHM8oH3XKbqnv6fvJoEb4LyHJqcpAQHSiyTTQKLuE45dlP hJHJ8eWaR/94VRUVG+ZfbTA5ydcBCxn4Lho/+xzBPwGfKxdCNX9UJ7tjVvfZ0fZ7AVP7 npSqaQFbGBqGNuAclflcWRBOhUJME0iGfAfz6nBO7DpjdTVieBcTlvmWbgyIYyQBzY4z 6R5lQFw9rOWL8/GQcGXl2hQOR4xf72N71U4pMCwP706R2rSdX7IzOU8wJwhBdAnoxxBO IlS2wlqTRrFBChn8Fndkjg7nc4W2jYyeyZ4Ty/W5n6ViN23jACOMGehUvvbuBraNl8f3 h7Tg== X-Gm-Message-State: AGi0PuarYbM+yld5KViVFPbNL8phAgrKAbk7A/1E197Pkwo85oE15utr YxSZchyrP6yO0jjYp4wAE9DZEg== X-Google-Smtp-Source: APiQypKCpubmYvC/OrHLHAy3Hs14HLFfQ51lPxMeGFhqpteEmJI+Q2j4qlF2dWKK85YyeUtNBIlN+A== X-Received: by 2002:a17:902:507:: with SMTP id 7mr4332621plf.42.1586296840284; Tue, 07 Apr 2020 15:00:40 -0700 (PDT) Received: from tictac2.mtv.corp.google.com ([2620:15c:202:1:24fa:e766:52c9:e3b2]) by smtp.gmail.com with ESMTPSA id g3sm880112pgd.64.2020.04.07.15.00.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Apr 2020 15:00:39 -0700 (PDT) From: Douglas Anderson To: axboe@kernel.dk, jejb@linux.ibm.com, martin.petersen@oracle.com Cc: Ming Lei , linux-scsi@vger.kernel.org, sqazi@google.com, Gwendal Grignou , groeck@chromium.org, linux-block@vger.kernel.org, paolo.valente@linaro.org, Douglas Anderson , =?utf-8?q?Andr=C3=A9_Almeida?= , Bart Van Assche , Damien Le Moal , John Garry , Pavel Begunkov , Sagi Grimberg , linux-kernel@vger.kernel.org Subject: [PATCH v3 2/4] blk-mq: Add blk_mq_delay_run_hw_queues() API call Date: Tue, 7 Apr 2020 15:00:03 -0700 Message-Id: <20200407145906.v3.2.I4c665d70212a5b33e103fec4d5019a59b4c05577@changeid> X-Mailer: git-send-email 2.26.0.292.g33ef6b2f38-goog In-Reply-To: <20200407220005.119540-1-dianders@chromium.org> References: <20200407220005.119540-1-dianders@chromium.org> MIME-Version: 1.0 Sender: linux-scsi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org We have: * blk_mq_run_hw_queue() * blk_mq_delay_run_hw_queue() * blk_mq_run_hw_queues() ...but not blk_mq_delay_run_hw_queues(), presumably because nobody needed it before now. Since we need it for a later patch in this series, add it. Signed-off-by: Douglas Anderson --- Changes in v3: - ("blk-mq: Add blk_mq_delay_run_hw_queues() API call") new for v3 Changes in v2: None block/blk-mq.c | 19 +++++++++++++++++++ include/linux/blk-mq.h | 1 + 2 files changed, 20 insertions(+) diff --git a/block/blk-mq.c b/block/blk-mq.c index 7d5f388b7da9..f0a89109fb6d 100644 --- a/block/blk-mq.c +++ b/block/blk-mq.c @@ -1544,6 +1544,25 @@ void blk_mq_run_hw_queues(struct request_queue *q, bool async) } EXPORT_SYMBOL(blk_mq_run_hw_queues); +/** + * blk_mq_delay_run_hw_queues - Run all hardware queues asynchronously. + * @q: Pointer to the request queue to run. + * @msecs: Microseconds of delay to wait before running the queues. + */ +void blk_mq_delay_run_hw_queues(struct request_queue *q, unsigned long msecs) +{ + struct blk_mq_hw_ctx *hctx; + int i; + + queue_for_each_hw_ctx(q, hctx, i) { + if (blk_mq_hctx_stopped(hctx)) + continue; + + blk_mq_delay_run_hw_queue(hctx, msecs); + } +} +EXPORT_SYMBOL(blk_mq_delay_run_hw_queues); + /** * blk_mq_queue_stopped() - check whether one or more hctxs have been stopped * @q: request queue. diff --git a/include/linux/blk-mq.h b/include/linux/blk-mq.h index f389d7c724bd..3bbc730eca72 100644 --- a/include/linux/blk-mq.h +++ b/include/linux/blk-mq.h @@ -508,6 +508,7 @@ void blk_mq_unquiesce_queue(struct request_queue *q); void blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs); void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async); void blk_mq_run_hw_queues(struct request_queue *q, bool async); +void blk_mq_delay_run_hw_queues(struct request_queue *q, unsigned long msecs); void blk_mq_tagset_busy_iter(struct blk_mq_tag_set *tagset, busy_tag_iter_fn *fn, void *priv); void blk_mq_tagset_wait_completed_request(struct blk_mq_tag_set *tagset); From patchwork Tue Apr 7 22:00:04 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Doug Anderson X-Patchwork-Id: 11479025 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 37756913 for ; Tue, 7 Apr 2020 22:00:51 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 0D1E92082D for ; Tue, 7 Apr 2020 22:00:51 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="DoMYYcsn" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726559AbgDGWAn (ORCPT ); Tue, 7 Apr 2020 18:00:43 -0400 Received: from mail-pg1-f193.google.com ([209.85.215.193]:35434 "EHLO mail-pg1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726520AbgDGWAm (ORCPT ); Tue, 7 Apr 2020 18:00:42 -0400 Received: by mail-pg1-f193.google.com with SMTP id k5so2380088pga.2 for ; Tue, 07 Apr 2020 15:00:42 -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=Me9YjeaVzWgazxTUthRuKJI46sDGdqwI3du7HWGuOE8=; b=DoMYYcsntVvHTK2oXwnp3vDDbf3nT3otp0Pm+CFLtccf3kJadBX/hZRhcI+IUM7OHu 9GtnDCkyi0neXGAopex9ODL02nb2SkLNI9F9jFhOpGDWQH8JSCPaQyp2PFwBCyKzwP31 RESBufGdSgg25ZKaFUfV1vNerMXA6d58coYtU= 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=Me9YjeaVzWgazxTUthRuKJI46sDGdqwI3du7HWGuOE8=; b=YYesFV9yFX70LpjC4TsUi76m1vkddN5m/+/qtmndLSvjW4AW6IhT6pyQv9gTAznrzy rCUUd+YatXcsabaIu+9vN1wCj1mgJjPDzpbCmf9D/IE8hRlSs3DdB2NPA1x2VQ+U1c5T MptdJCFa9syQ6qyqSWeLWrgT8SwflBDSTpVLb9MluvX6hvXGpZGkCQI64U8RVMurFLSZ c8iiWot9eb6DceNRzQScSdY2g/4DGgeCnlWcPZrblAtBP1557fMfIVcqyvCk0rP8LAKu kPHJRa3syZtOMedBmyPGuijEjnU2tttzSM9BnVR87y/5n+CmLdQopR2LlofNpeRWmKBr LXgw== X-Gm-Message-State: AGi0PuZdw4hKe7DviDgG9VHIWr9vn5kGMJZCWd6k+36yWBsmivD5ix4G F8Fg+pY+skGE7Wzs8eUiomjZbA== X-Google-Smtp-Source: APiQypIc2emGqDVPItIPl9ujfmle5KLS4GyLNUOKdjuBUMmxWIOMYhRxDK6Fup63PD2P+ZhrpVonLQ== X-Received: by 2002:a63:c06:: with SMTP id b6mr4046203pgl.218.1586296841645; Tue, 07 Apr 2020 15:00:41 -0700 (PDT) Received: from tictac2.mtv.corp.google.com ([2620:15c:202:1:24fa:e766:52c9:e3b2]) by smtp.gmail.com with ESMTPSA id g3sm880112pgd.64.2020.04.07.15.00.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Apr 2020 15:00:41 -0700 (PDT) From: Douglas Anderson To: axboe@kernel.dk, jejb@linux.ibm.com, martin.petersen@oracle.com Cc: Ming Lei , linux-scsi@vger.kernel.org, sqazi@google.com, Gwendal Grignou , groeck@chromium.org, linux-block@vger.kernel.org, paolo.valente@linaro.org, Douglas Anderson , linux-kernel@vger.kernel.org Subject: [PATCH v3 3/4] blk-mq: Rerun dispatching in the case of budget contention Date: Tue, 7 Apr 2020 15:00:04 -0700 Message-Id: <20200407145906.v3.3.I28278ef8ea27afc0ec7e597752a6d4e58c16176f@changeid> X-Mailer: git-send-email 2.26.0.292.g33ef6b2f38-goog In-Reply-To: <20200407220005.119540-1-dianders@chromium.org> References: <20200407220005.119540-1-dianders@chromium.org> MIME-Version: 1.0 Sender: linux-scsi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org If ever a thread running blk-mq code tries to get budget and fails it immediately stops doing work and assumes that whenever budget is freed up that queues will be kicked and whatever work the thread was trying to do will be tried again. One path where budget is freed and queues are kicked in the normal case can be seen in scsi_finish_command(). Specifically: - scsi_finish_command() - scsi_device_unbusy() - # Decrement "device_busy", AKA release budget - scsi_io_completion() - scsi_end_request() - blk_mq_run_hw_queues() The above is all well and good. The problem comes up when a thread claims the budget but then releases it without actually dispatching any work. Since we didn't schedule any work we'll never run the path of finishing work / kicking the queues. This isn't often actually a problem which is why this issue has existed for a while and nobody noticed. Specifically we only get into this situation when we unexpectedly found that we weren't going to do any work. Code that later receives new work kicks the queues. All good, right? The problem shows up, however, if timing is just wrong and we hit a race. To see this race let's think about the case where we only have a budget of 1 (only one thread can hold budget). Now imagine that a thread got budget and then decided not to dispatch work. It's about to call put_budget() but then the thread gets context switched out for a long, long time. While in this state, any and all kicks of the queue (like the when we received new work) will be no-ops because nobody can get budget. Finally the thread holding budget gets to run again and returns. All the normal kicks will have been no-ops and we have an I/O stall. As you can see from the above, you need just the right timing to see the race. To start with, the only case it happens if we thought we had work, actually managed to get the budget, but then actually didn't have work. That's pretty rare to start with. Even then, there's usually a very small amount of time between realizing that there's no work and putting the budget. During this small amount of time new work has to come in and the queue kick has to make it all the way to trying to get the budget and fail. It's pretty unlikely. One case where this could have failed is illustrated by an example of threads running blk_mq_do_dispatch_sched(): * Threads A and B both run has_work() at the same time with the same "hctx". Imagine has_work() is exact. There's no lock, so it's OK if Thread A and B both get back true. * Thread B gets interrupted for a long time right after it decides that there is work. Maybe its CPU gets an interrupt and the interrupt handler is slow. * Thread A runs, get budget, dispatches work. * Thread A's work finishes and budget is released. * Thread B finally runs again and gets budget. * Since Thread A already took care of the work and no new work has come in, Thread B will get NULL from dispatch_request(). I believe this is specifically why dispatch_request() is allowed to return NULL in the first place if has_work() must be exact. * Thread B will now be holding the budget and is about to call put_budget(), but hasn't called it yet. * Thread B gets interrupted for a long time (again). Dang interrupts. * Now Thread C (maybe with a different "hctx" but the same queue) comes along and runs blk_mq_do_dispatch_sched(). * Thread C won't do anything because it can't get budget. * Finally Thread B will run again and put the budget without kicking any queues. Even though the example above is with blk_mq_do_dispatch_sched() I believe the race is possible any time someone is holding budget but doesn't do work. Unfortunately, the unlikely has become more likely if you happen to be using the BFQ I/O scheduler. BFQ, by design, sometimes returns "true" for has_work() but then NULL for dispatch_request() and stays in this state for a while (currently up to 9 ms). Suddenly you only need one race to hit, not two races in a row. With my current setup this is easy to reproduce in reboot tests and traces have actually shown that we hit a race similar to the one describe above. In theory we could choose to just fix blk_mq_do_dispatch_sched() to kick the queues when it puts budget. That would fix the BFQ case and one could argue that all the other cases are just theoretical. While that is true, for all the other cases it should be very uncommon to run into the case where we need put_budget(). Having an extra queue kick for safety there shouldn't affect much and keeps the race at bay. One last note is that (at least in the SCSI case) budget is shared by all "hctx"s that have the same queue. Thus we need to make sure to kick the whole queue, not just re-run dispatching on a single "hctx". Signed-off-by: Douglas Anderson --- Changes in v3: - Always kick when putting the budget. - Delay blk_mq_do_dispatch_sched() kick by 3 ms for inexact has_work(). - Totally rewrote commit message. Changes in v2: - Replace ("scsi: core: Fix stall...") w/ ("blk-mq: Rerun dispatch...") block/blk-mq.h | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/block/blk-mq.h b/block/blk-mq.h index 10bfdfb494fa..1270505367ab 100644 --- a/block/blk-mq.h +++ b/block/blk-mq.h @@ -180,12 +180,24 @@ unsigned int blk_mq_in_flight(struct request_queue *q, struct hd_struct *part); void blk_mq_in_flight_rw(struct request_queue *q, struct hd_struct *part, unsigned int inflight[2]); +#define BLK_MQ_BUDGET_DELAY 3 /* ms units */ + static inline void blk_mq_put_dispatch_budget(struct blk_mq_hw_ctx *hctx) { struct request_queue *q = hctx->queue; - if (q->mq_ops->put_budget) + if (q->mq_ops->put_budget) { q->mq_ops->put_budget(hctx); + + /* + * The only time we call blk_mq_put_dispatch_budget() is if + * we released the budget without dispatching. Holding the + * budget could have blocked any "hctx"s with the same queue + * and if we didn't dispatch then there's no guarantee anyone + * will kick the queue. Kick it ourselves. + */ + blk_mq_delay_run_hw_queues(q, BLK_MQ_BUDGET_DELAY); + } } static inline bool blk_mq_get_dispatch_budget(struct blk_mq_hw_ctx *hctx) From patchwork Tue Apr 7 22:00:05 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Doug Anderson X-Patchwork-Id: 11479019 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D23891744 for ; Tue, 7 Apr 2020 22:00:49 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id AFF2720747 for ; Tue, 7 Apr 2020 22:00:49 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="gy9VuPQk" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726475AbgDGWAs (ORCPT ); Tue, 7 Apr 2020 18:00:48 -0400 Received: from mail-pf1-f193.google.com ([209.85.210.193]:37083 "EHLO mail-pf1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726534AbgDGWAq (ORCPT ); Tue, 7 Apr 2020 18:00:46 -0400 Received: by mail-pf1-f193.google.com with SMTP id u65so1432064pfb.4 for ; Tue, 07 Apr 2020 15:00:43 -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=Va+2nc4i3Yd/W+iIcLatzp6wimnXHUJvgsPfQPVfjOk=; b=gy9VuPQkWxTJk5f0NPImzjXAgbmkdVnSGu/aCOlfUYUONmXPV34RUXTT3IJ7hCCZeZ qk0UN2+a0tnubLFizwXeReioGyiMApRKwoHrXd6DWPlY8tn3qMLBbhUo5cwYMalX/UWJ d0V3h9CexwCSBJhcf92nXnfL5uzWNpMz3ppyM= 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=Va+2nc4i3Yd/W+iIcLatzp6wimnXHUJvgsPfQPVfjOk=; b=lK+aP5kzSfhOOKQg110gKOpECNewr9XrQIVNjOcWY6VuYroj2M0qLXz4EAOCn59Rc2 +w79pEbCveFN/mxlZBQICxETJeBjxJPNft0GP6lAAzxIbwv4ZYt6/L3JnFA66gOP0gtK 629XU5F0CZv7NMnlSiBeMTkY6m+DIPkAtl6P8tE2EAduPFPtYuLosKJ804PY7u4AlVgy xqYwxj0X1CC4D2CCOukL98kaUJXBip3dgolPSy/slZsQttmOHRjvdZnCoo3ZeDWyAn32 Xp0qfNIGLYOcoWeQLf2a0DcuFHYpcKioG5wXZlIGKRVjrpLLcLuL1MpKk7eTIOc0JyZB i/Lw== X-Gm-Message-State: AGi0PubwuK0er/wROcxj+XNjeO32dP2ZxcxX623GW6/UjfxnE9EQwiBs 14YJjD8w673yZg6bKPKWkYEovA== X-Google-Smtp-Source: APiQypL2Qu8KYDrWW/tOAFF4GfWMTLWK8Gme7f6J6H+6CiWntfa0rfCy/dU1fPn7+ho9ZB99Vqc/kA== X-Received: by 2002:a63:e350:: with SMTP id o16mr3931135pgj.250.1586296842988; Tue, 07 Apr 2020 15:00:42 -0700 (PDT) Received: from tictac2.mtv.corp.google.com ([2620:15c:202:1:24fa:e766:52c9:e3b2]) by smtp.gmail.com with ESMTPSA id g3sm880112pgd.64.2020.04.07.15.00.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Apr 2020 15:00:42 -0700 (PDT) From: Douglas Anderson To: axboe@kernel.dk, jejb@linux.ibm.com, martin.petersen@oracle.com Cc: Ming Lei , linux-scsi@vger.kernel.org, sqazi@google.com, Gwendal Grignou , groeck@chromium.org, linux-block@vger.kernel.org, paolo.valente@linaro.org, Douglas Anderson , linux-kernel@vger.kernel.org Subject: [PATCH v3 4/4] Revert "scsi: core: run queue if SCSI device queue isn't ready and queue is idle" Date: Tue, 7 Apr 2020 15:00:05 -0700 Message-Id: <20200407145906.v3.4.I630e6ca4cdcf9ab13ea899274745f9e3174eb12b@changeid> X-Mailer: git-send-email 2.26.0.292.g33ef6b2f38-goog In-Reply-To: <20200407220005.119540-1-dianders@chromium.org> References: <20200407220005.119540-1-dianders@chromium.org> MIME-Version: 1.0 Sender: linux-scsi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org This reverts commit 7e70aa789d4a0c89dbfbd2c8a974a4df717475ec. Now that we have the patches ("blk-mq: In blk_mq_dispatch_rq_list() "no budget" is a reason to kick") and ("blk-mq: Rerun dispatching in the case of budget contention") we should no longer need the fix in the SCSI code. Revert it, resolving conflicts with other patches that have touched this code. With this revert (and the two new patches) I can run the script that was in commit 7e70aa789d4a ("scsi: core: run queue if SCSI device queue isn't ready and queue is idle") in a loop with no failure. If I do this revert without the two new patches I can easily get a failure. Signed-off-by: Douglas Anderson --- I don't know for sure that we can revert this patch, but in the very least the original test case now passes. If there is any question about this, we can just drop this patch. Changes in v3: - ("Revert "scsi: core: run queue...") new for v3. Changes in v2: None drivers/scsi/scsi_lib.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/drivers/scsi/scsi_lib.c b/drivers/scsi/scsi_lib.c index 47835c4b4ee0..ea18f618dc66 100644 --- a/drivers/scsi/scsi_lib.c +++ b/drivers/scsi/scsi_lib.c @@ -1610,12 +1610,7 @@ static bool scsi_mq_get_budget(struct blk_mq_hw_ctx *hctx) struct request_queue *q = hctx->queue; struct scsi_device *sdev = q->queuedata; - if (scsi_dev_queue_ready(q, sdev)) - return true; - - if (atomic_read(&sdev->device_busy) == 0 && !scsi_device_blocked(sdev)) - blk_mq_delay_run_hw_queue(hctx, SCSI_QUEUE_DELAY); - return false; + return scsi_dev_queue_ready(q, sdev); } static blk_status_t scsi_queue_rq(struct blk_mq_hw_ctx *hctx,