From nobody Sun Feb 8 18:43:35 2026 Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B3FCF3D982 for ; Sun, 11 Aug 2024 10:19:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723371579; cv=none; b=lDgH6TUhUPWaOYG991Szlq3eOnHigdWjGL7vn6UVMROHynjjDpX8gpryB8oo8cPLeozH695m0+ll6vic9/d7Xtc7TrM5+gGHzhp+E9WIcGc2xZSYdyBBhfFfd+oy04yDiZ7ZnPsK9lJPyuVztvYumfNRxDLzkcseKiV4nFk+2Ss= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723371579; c=relaxed/simple; bh=jYIWF+d8RtT7CApAhcVVOIWuKzK336s2dIOxS36Vebo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Y9H6JbxYSL04QkXZGVpbflIQjzKcx2GzxEP+NOuxWivQ5aM+XvdSGOmsPnsyx3mnAag+hUewxtD1TCsUiVJo08BcTZJkgVN9Bd0etMYPcY1Wsn9QKcVMx4Do/gn/R3cucs4V9KQ4+ulAYytQuj7RYS1JhWKD+Z1iGd6ZDtaCNGk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com; spf=pass smtp.mailfrom=bytedance.com; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b=Es5d83oS; arc=none smtp.client-ip=209.85.214.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bytedance.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b="Es5d83oS" Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-1ff4fa918afso20372765ad.1 for ; Sun, 11 Aug 2024 03:19:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1723371577; x=1723976377; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ds1ze48w37yLgthqPugWbimWqZx+HO9a59JENpUWbI8=; b=Es5d83oS7dIE8k3gu8bnmEXEzY0ILq8dlhLPRwcOaE+mZB5NWye0KCTTRJnnhcXSMI dGScq+I4CgBBJUGMobo+AaJXidin9E+eotvaSevl+wjFFH4b/INU+Ps6m2uiPljhq0c8 B/8Xo8HdbK04PVYCPd5UPhJc5lVwgyjAV5tq0Zc2f726EfVygyHCRdWg7zqW1xsmAGdu 5+6Feo9/D1I7Gwe+w7u9OjNqvotrk3SUuka+wwWlzzfL+BDf/RhvoOWOOcE+byRLyYBI f8ANMxKoLAYxCVxWNY0el1OP0rCzAoaYGH15VIPA0+nsR2Y5NxXB/MyxHO/iHyg/RW+8 LMiA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723371577; x=1723976377; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ds1ze48w37yLgthqPugWbimWqZx+HO9a59JENpUWbI8=; b=rDacug2jMj8GXYT0xxfaOy85lfdhvyOAlQxV/WT2JuWxFqxZUCp3oHozETKoD+UvVg DpBAfoZMOet13ZoX3EoV2nrX7AAWCy2aS0POBS/fxUEHYcdtYM49yG4N7zkGCnNWwffS eMf4vx4uSnF7wWEGuC3jHTpIgQejcSf2QQgu8R27VD78bIJpO6nIzYcO5zppPnlTvDbU PP6ugWZfN9X7DeblaYYCM+jZoc4b6SEEXAPJmtGI+aNditZjQD+kbWAmZuCXXZ5qYLil DAzXYKbsMyyaKYP0ry4IkTbhm49GKu+YggC2G1Nfa2i/jJ5u/xQcSBPV7KcJDzCodHD8 5gMg== X-Forwarded-Encrypted: i=1; AJvYcCVdByEf4tKyBbd4AK3dJwOT7SUeVluifgR7KDToRMeaxlZIP1UYhKXNUO5YHLr+qpskyhvlaVg2dUG4nQY7I9zR3k0dIUAyWd0zOe+q X-Gm-Message-State: AOJu0YyUGDsxumHG/QEe2buvkOCIELQxyaIkOFBdGrBbnzhb9WxPEIa5 rJivgYtjwP7EQj0Mh6UaNU0dax1AdRoCx8dZk5uR98HQ4CVFWakGEBpeINP9PKs= X-Google-Smtp-Source: AGHT+IHrhGO7FADS+Xso42jN4oliqSfyeCN/hga1nn4ByOZpRGS4MDU9GUc15B1hCpOlbLBBamKaog== X-Received: by 2002:a17:903:41d2:b0:1fa:7e0:d69a with SMTP id d9443c01a7336-200ae5cf5b0mr55765245ad.46.1723371576935; Sun, 11 Aug 2024 03:19:36 -0700 (PDT) Received: from PXLDJ45XCM.bytedance.net ([139.177.225.239]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-200bbb48b81sm20992155ad.297.2024.08.11.03.19.34 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sun, 11 Aug 2024 03:19:36 -0700 (PDT) From: Muchun Song To: axboe@kernel.dk Cc: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, Muchun Song Subject: [PATCH 1/4] block: fix request starvation when queue is stopped or quiesced Date: Sun, 11 Aug 2024 18:19:18 +0800 Message-Id: <20240811101921.4031-2-songmuchun@bytedance.com> X-Mailer: git-send-email 2.39.3 (Apple Git-146) In-Reply-To: <20240811101921.4031-1-songmuchun@bytedance.com> References: <20240811101921.4031-1-songmuchun@bytedance.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Supposing the following scenario with a virtio_blk driver. CPU0 CPU1 = CPU2 blk_mq_try_issue_directly() __blk_mq_issue_directly() q->mq_ops->queue_rq() virtio_queue_rq() blk_mq_stop_hw_queue() blk_mq_try_issue_directly() = virtblk_done() if (blk_mq_hctx_stopped()) blk_mq_request_bypass_insert() = blk_mq_start_stopped_hw_queue() blk_mq_run_hw_queue() = blk_mq_run_hw_queue() blk_mq_insert_request() return // Who is responsibl= e for dispatching this IO request? After CPU0 has marked the queue as stopped, CPU1 will see the queue is stop= ped. But before CPU1 puts the request on the dispatch list, CPU2 receives the in= terrupt of completion of request, so it will run the hardware queue and marks the q= ueue as non-stopped. Meanwhile, CPU1 also runs the same hardware queue. After bo= th CPU1 and CPU2 complete blk_mq_run_hw_queue(), CPU1 just puts the request to the = same hardware queue and returns. Seems it misses dispatching a request. Fix it by running the hardware queue explicitly. I think blk_mq_request_issue_directl= y() should handle a similar problem. Signed-off-by: Muchun Song Reviewed-by: Ming Lei --- block/blk-mq.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/block/blk-mq.c b/block/blk-mq.c index e3c3c0c21b553..b2d0f22de0c7f 100644 --- a/block/blk-mq.c +++ b/block/blk-mq.c @@ -2619,6 +2619,7 @@ static void blk_mq_try_issue_directly(struct blk_mq_h= w_ctx *hctx, =20 if (blk_mq_hctx_stopped(hctx) || blk_queue_quiesced(rq->q)) { blk_mq_insert_request(rq, 0); + blk_mq_run_hw_queue(hctx, false); return; } =20 @@ -2649,6 +2650,7 @@ static blk_status_t blk_mq_request_issue_directly(str= uct request *rq, bool last) =20 if (blk_mq_hctx_stopped(hctx) || blk_queue_quiesced(rq->q)) { blk_mq_insert_request(rq, 0); + blk_mq_run_hw_queue(hctx, false); return BLK_STS_OK; } =20 --=20 2.20.1 From nobody Sun Feb 8 18:43:35 2026 Received: from mail-oi1-f178.google.com (mail-oi1-f178.google.com [209.85.167.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BAB73481CE for ; Sun, 11 Aug 2024 10:19:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723371582; cv=none; b=lxutwZ2aDmx4IWN92Dn+w8nUP4MLPEayGeugXLkQXShwoTpntKIbl8/7BibsdJgmJV90tHQqPRNogCp0UPIAimEyG9end+VWEJeP1qeEqV622gIv1LwiVphjBjxZvHM/7n1Uje3Of3ICD9meoKgY1DqLzSiwtL+RPGJNnl2T034= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723371582; c=relaxed/simple; bh=6pftb2nnNbxoYTrue2Jw5X76kBNH8jDfyjIN9qpxI1c=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=BNg7ljbgWpLbMU51ADo3yTUos4bfTIPt87nk+Q4AhCM0SYIu4dn5ruMyDA8xxry+vZ6T7zcbaWIBc54C1MQKrfra2mXMNXxAVFOTJMKSR8PHx35LPrBP71id7ZLFj4p/Qw8F7Cl4nNCs0065w2PBtWQRrVrLAohgNdGmiK6pX64= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com; spf=pass smtp.mailfrom=bytedance.com; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b=XXAkHmvb; arc=none smtp.client-ip=209.85.167.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bytedance.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b="XXAkHmvb" Received: by mail-oi1-f178.google.com with SMTP id 5614622812f47-3db16b2c1d2so2661377b6e.2 for ; Sun, 11 Aug 2024 03:19:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1723371580; x=1723976380; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=8nvX3wlUypt+6rCQzwo1fpItqGzwm6bo/2mQyCkFK9s=; b=XXAkHmvbt1ZgL+N+KEuYtJsOrMixC5FPNa1YEInbx6I+R2Ke+Q2gIM+QgZoNTC3eqK xOPWXRmSZlQHlHphWNrRME2zuNuOCFm01GDi8Eaf1kyfhZuNQH7Coe1e+COrhAPHHL7a pC4PStqaZ7eNKiEH8QaT+vSv5eUDkhUQb9ZcP044sG3gH2YFaezVCxrUT2HZhkZaXdqW fYQSsIJQCmwv71WY3MjycM8x8GEcilUYj+IFEjDCAirMVCtgoNkWAsJykhehQ/shbblJ MDUJD7F5kIiGOIluoE3yhYDZBXdO/S+uJovNfmm06+J71C3f1nsJqCjpnlMwfmY5Q0t1 rawQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723371580; x=1723976380; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=8nvX3wlUypt+6rCQzwo1fpItqGzwm6bo/2mQyCkFK9s=; b=YJlcs5jqVYGZJeD6rlp8l0H42QTZRS6GZPO3f/oV6tkQY9j4l+hk31QVbBkL932+RM Br6U1nlflaNgU3URHMohUtQgRAWvSUwrEmEYUtPfcdX/Ag2vCB4DSv2idY22ba0TDDDe HZy9ek1yX0PcbQ13LvUC0C/Urr1WToSk9T/KErWJMkPY687575nfgVv9DI2DCzzPEIB8 Uq3IXvpL4UOmDpgXwsqb5FHfQV0PNgzECxlcdCcAM2V1DmCg0GL7WOPToYxc5DdhI34t E/Tnf7P62Ym9fmMmz1kYQPD28NH3EOsO7tlllq3bEuV3dx0pD+fVwFaP3pht4VimDH6o 4fZw== X-Forwarded-Encrypted: i=1; AJvYcCUrHfu5xWzph5DmMYrHtIC2uW/ukNO2lnaRaW08+YJvokrfOSR4jDQvRYDhXVoIt71ZMEt3i8IVbRUbe8C6XGzzdYq9bcLhd8A0WVPY X-Gm-Message-State: AOJu0YwmuJlfglahkgjN+sMtCxyyUHzGQvrI9zLMUe5tfUvUvrN/JBiY rXhV/dfd3Htb/WF0Q6ZPDTYrXlZjjG9TPYm/q2PKLE1OrSBZ5oSgDrLttH3vy/LVWZFsdbeXrD0 F X-Google-Smtp-Source: AGHT+IGNBvMJelhnYpGmxkSKFmzcolA3NBJvUuOHdFqKSBrLXqP7AFYt9sICFSkTLaLIlAD7xmrYZw== X-Received: by 2002:a05:6808:30a5:b0:3da:ae19:ef0 with SMTP id 5614622812f47-3dc417059e8mr9311323b6e.49.1723371579831; Sun, 11 Aug 2024 03:19:39 -0700 (PDT) Received: from PXLDJ45XCM.bytedance.net ([139.177.225.239]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-200bbb48b81sm20992155ad.297.2024.08.11.03.19.37 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sun, 11 Aug 2024 03:19:39 -0700 (PDT) From: Muchun Song To: axboe@kernel.dk Cc: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, Muchun Song Subject: [PATCH 2/4] block: fix ordering between checking BLK_MQ_S_STOPPED and adding requests to hctx->dispatch Date: Sun, 11 Aug 2024 18:19:19 +0800 Message-Id: <20240811101921.4031-3-songmuchun@bytedance.com> X-Mailer: git-send-email 2.39.3 (Apple Git-146) In-Reply-To: <20240811101921.4031-1-songmuchun@bytedance.com> References: <20240811101921.4031-1-songmuchun@bytedance.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Supposing the following scenario with a virtio_blk driver. CPU0 CPU1 blk_mq_try_issue_directly() __blk_mq_issue_directly() q->mq_ops->queue_rq() virtio_queue_rq() blk_mq_stop_hw_queue() virtblk= _done() blk_mq_request_bypass_insert() blk= _mq_start_stopped_hw_queues() /* Add IO request to dispatch list */ 1) store = blk_mq_start_stopped_hw_queue() = clear_bit(BLK_MQ_S_STOPPED) 3) store blk_mq_run_hw_queue() = blk_mq_run_hw_queue() if (!blk_mq_hctx_has_pending()) = if (!blk_mq_hctx_has_pending()) 4) load return = return blk_mq_sched_dispatch_requests() = blk_mq_sched_dispatch_requests() if (blk_mq_hctx_stopped()) 2) load = if (blk_mq_hctx_stopped()) return = return __blk_mq_sched_dispatch_requests() = __blk_mq_sched_dispatch_requests() The full memory barrier should be inserted between 1) and 2), as well as be= tween 3) and 4) to make sure that either CPU0 sees BLK_MQ_S_STOPPED is cleared or= CPU1 sees dispatch list or setting of bitmap of software queue. Otherwise, eithe= r CPU will not re-run the hardware queue causing starvation. Signed-off-by: Muchun Song --- block/blk-mq.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/block/blk-mq.c b/block/blk-mq.c index b2d0f22de0c7f..6f18993b8f454 100644 --- a/block/blk-mq.c +++ b/block/blk-mq.c @@ -2075,6 +2075,13 @@ bool blk_mq_dispatch_rq_list(struct blk_mq_hw_ctx *h= ctx, struct list_head *list, * in blk_mq_sched_restart(). Avoid restart code path to * miss the new added requests to hctx->dispatch, meantime * SCHED_RESTART is observed here. + * + * This barrier is also used to order adding of dispatch list + * above and the test of BLK_MQ_S_STOPPED in the following + * routine (in blk_mq_delay_run_hw_queue()). Pairs with the + * barrier in blk_mq_start_stopped_hw_queue(). So dispatch code + * could either see BLK_MQ_S_STOPPED is cleared or dispatch list + * to avoid missing dispatching requests. */ smp_mb(); =20 @@ -2237,6 +2244,17 @@ void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx,= bool async) if (!need_run) return; =20 + /* + * This barrier is used to order adding of dispatch list or setting + * of bitmap of any software queue outside of this function and the + * test of BLK_MQ_S_STOPPED in the following routine. Pairs with the + * barrier in blk_mq_start_stopped_hw_queue(). So dispatch code could + * either see BLK_MQ_S_STOPPED is cleared or dispatch list or setting + * of bitmap of any software queue to avoid missing dispatching + * requests. + */ + smp_mb(); + if (async || !cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask)) { blk_mq_delay_run_hw_queue(hctx, 0); return; @@ -2392,6 +2410,13 @@ void blk_mq_start_stopped_hw_queue(struct blk_mq_hw_= ctx *hctx, bool async) return; =20 clear_bit(BLK_MQ_S_STOPPED, &hctx->state); + /* + * Pairs with the smp_mb() in blk_mq_run_hw_queue() or + * blk_mq_dispatch_rq_list() to order the clearing of + * BLK_MQ_S_STOPPED and the test of dispatch list or + * bitmap of any software queue. + */ + smp_mb__after_atomic(); blk_mq_run_hw_queue(hctx, async); } EXPORT_SYMBOL_GPL(blk_mq_start_stopped_hw_queue); --=20 2.20.1 From nobody Sun Feb 8 18:43:35 2026 Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C00684D8AE for ; Sun, 11 Aug 2024 10:19:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723371585; cv=none; b=I+TzrF7U1Ce8bIf4XC9jovQmW+aiR2+YkgTiv5wQXBlxuQ1XompQz23etSGyOsOZB25JOa2Bzp5fTQGH9b+I5rjTRCqHYBdLj22Q2pmpSk3EGhrrkjtFIRJMU5RS6Tbibc5b3pPXKZ7ElrjmqCKI7fhll2L+VYSexuqdEqRNUgU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723371585; c=relaxed/simple; bh=/1eM/KOiyGmp7R3yAOpaP4qJdimrFBpaEBE6Erne01k=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=kMIkLJZYNdpjX0CbDjIP7BRhvR7mlpviMSi1/pO8Hfn0BOGT3JSn9iZdGQl0NI1x+7/XAK7B1mv57jcWbwCOhva370yOTlpAYgHj7r7+ZylVsMSLUwa8kATcx/LpAa9PQitCUjDgUZE3dSuhufuDIUl3EUBTxzX2Mr8Pc8d25g0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com; spf=pass smtp.mailfrom=bytedance.com; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b=S2LqvYBy; arc=none smtp.client-ip=209.85.214.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bytedance.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b="S2LqvYBy" Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-1fc4fccdd78so24659805ad.2 for ; Sun, 11 Aug 2024 03:19:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1723371583; x=1723976383; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=PIZMdYQrQP44xaCgPEOO0G1aVB5mHBqSKoNfWGusOBY=; b=S2LqvYByH6OxwVMYRv9+E5xUu7m2fik90U84BdZpvgFlYYPO8QjSXs6xEgxSIDVtE7 ILDhh7M1UAd6H8PAEm1NOdBuHQsvn5cueHzA8rx5LZQPkl03LkGwttCghjdx6WUncdoA FLuy9vUVe0+WF5nEnGrIB31uq8LoAOADiAirjEs1ecKyUWPNbDfcluZw6Y2amKyRMpgF kzWY+iGPhyL5mSKgVUY/kcdSC4oQN0Pr2iCj6Ai6jb+TKRofKxOv/lMulWJ/CKo2MmYr tOeijUBAjWEKGUwEBO9yWRig82D1NpINxk9djJpTK9wyNXnDMgHSdjg2f8cLNaA5T81z cPTw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723371583; x=1723976383; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=PIZMdYQrQP44xaCgPEOO0G1aVB5mHBqSKoNfWGusOBY=; b=mURYBP2KpAyO+YefdTJc35I1kVhABlr5tWftDML/Jm6+hno8UaW5rKf5TPGMDQBgbD AoEUb6hI1J+bAPLjMLqjGn+FP4a2RZL5EGTNtp0dSYMSCfu0ACPAOxfoddh69q49yWAL TAc0eDBZ1Bciw9DdMH85b4+fQofUY/atFKQ4utWVLycsI1oy7UOnCVENqBGUCxv3B0hy Ry6KTPP1GRFGHqn6/+QQcp1fmThRcQJz3rXLT3KtxvfKFZJSdDL+k0cqa2LE3FUgEJp5 bYpNV8L1NCuLVk9czfqdO6cYXgrLzgGu2ReZIwKoUwE/sCCb3I/0MlvIMN4u61fJThlI gn/A== X-Forwarded-Encrypted: i=1; AJvYcCXd/OsNOGQ3DEQOLHypJK5gWOJpJU9HmujTbyF6vNA8xz2nw5cOHK7qRFwAYTp74H99pWqoM4ZyUIkWhv9PMhahTGJSeuvRjm7MQxn8 X-Gm-Message-State: AOJu0YydjBVfjGjt8FKdmz7EBRP8/r+PvLAXe6nqsW1R3TJyvHGc98IS IPhQgvVx5IMMcVxnFHN0mRE+Ve/AKmxf+tME3B6vxmAKCCTpsgzWgTdxwIrEREs= X-Google-Smtp-Source: AGHT+IEmgTi5qvtkHw8I1YhlxEDcSWuSsse8TTFfstC3EcNNkAaqrvnRF74AUV8RBR/aZ18EZX8wiQ== X-Received: by 2002:a17:902:ea12:b0:1fd:a503:88f0 with SMTP id d9443c01a7336-200ae597a39mr56929905ad.34.1723371582987; Sun, 11 Aug 2024 03:19:42 -0700 (PDT) Received: from PXLDJ45XCM.bytedance.net ([139.177.225.239]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-200bbb48b81sm20992155ad.297.2024.08.11.03.19.40 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sun, 11 Aug 2024 03:19:42 -0700 (PDT) From: Muchun Song To: axboe@kernel.dk Cc: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, Muchun Song Subject: [PATCH 3/4] block: fix missing smp_mb in blk_mq_{delay_}run_hw_queues Date: Sun, 11 Aug 2024 18:19:20 +0800 Message-Id: <20240811101921.4031-4-songmuchun@bytedance.com> X-Mailer: git-send-email 2.39.3 (Apple Git-146) In-Reply-To: <20240811101921.4031-1-songmuchun@bytedance.com> References: <20240811101921.4031-1-songmuchun@bytedance.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Supposing the following scenario with a virtio_blk driver. CPU0 CPU1 /* * Add request to dispatch list or set bitmap of * software queue. 1) store virtblk= _done() */ blk_mq_run_hw_queues()/blk_mq_delay_run_hw_queues() blk= _mq_start_stopped_hw_queues() if (blk_mq_hctx_stopped()) 2) load = blk_mq_start_stopped_hw_queue() continue = clear_bit(BLK_MQ_S_STOPPED) 3) store blk_mq_run_hw_queue()/blk_mq_delay_run_hw_queue() = blk_mq_run_hw_queue() = if (!blk_mq_hctx_has_pending()) 4) load = return = blk_mq_sched_dispatch_requests() The full memory barrier should be inserted between 1) and 2), as well as be= tween 3) and 4) to make sure that either CPU0 sees BLK_MQ_S_STOPPED is cleared or= CPU1 sees dispatch list or setting of bitmap of software queue. Otherwise, eithe= r CPU will not re-run the hardware queue causing starvation. Signed-off-by: Muchun Song --- block/blk-mq.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/block/blk-mq.c b/block/blk-mq.c index 6f18993b8f454..385a74e566874 100644 --- a/block/blk-mq.c +++ b/block/blk-mq.c @@ -2299,6 +2299,18 @@ void blk_mq_run_hw_queues(struct request_queue *q, b= ool async) sq_hctx =3D NULL; if (blk_queue_sq_sched(q)) sq_hctx =3D blk_mq_get_sq_hctx(q); + + /* + * This barrier is used to order adding of dispatch list or setting + * of bitmap of any software queue outside of this function and the + * test of BLK_MQ_S_STOPPED in the following routine. Pairs with the + * barrier in blk_mq_start_stopped_hw_queue(). So dispatch code could + * either see BLK_MQ_S_STOPPED is cleared or dispatch list or setting + * of bitmap of any software queue to avoid missing dispatching + * requests. + */ + smp_mb(); + queue_for_each_hw_ctx(q, hctx, i) { if (blk_mq_hctx_stopped(hctx)) continue; @@ -2327,6 +2339,18 @@ void blk_mq_delay_run_hw_queues(struct request_queue= *q, unsigned long msecs) sq_hctx =3D NULL; if (blk_queue_sq_sched(q)) sq_hctx =3D blk_mq_get_sq_hctx(q); + + /* + * This barrier is used to order adding of dispatch list or setting + * of bitmap of any software queue outside of this function and the + * test of BLK_MQ_S_STOPPED in the following routine. Pairs with the + * barrier in blk_mq_start_stopped_hw_queue(). So dispatch code could + * either see BLK_MQ_S_STOPPED is cleared or dispatch list or setting + * of bitmap of any software queue to avoid missing dispatching + * requests. + */ + smp_mb(); + queue_for_each_hw_ctx(q, hctx, i) { if (blk_mq_hctx_stopped(hctx)) continue; --=20 2.20.1 From nobody Sun Feb 8 18:43:35 2026 Received: from mail-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AD8835A0FE for ; Sun, 11 Aug 2024 10:19:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723371588; cv=none; b=gmq4D8NGodXh+3rCN00Ow+1uYTj6U+hHA3rZuF0G3OTOhJaugG69nKIWYmgNwJIrFTMaN62NlJYQQVY6swowFsHX+GEYdaiTsy+l5pR/XcSe3WFjgOrVulP9XguiLTir5y//1eFoCQMrN0qeFerllmZ5h2jEpopksUfB17wIkQM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723371588; c=relaxed/simple; bh=yv62PE+HHdpS6svbT+CRG1qAc/38LkE6TMF1vR09V/0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=iURvo9/UQoud/tJYdgdHm0l08X/XRJTAHGXLqNMOeExGcdXvgGsHAFiS0iFsPk3c6vs2/vXQp2Zhyyvf8uQMrcMAjabSb6lcsEhVr3ZtUIxdSEzsS5sYJQysPv9ifuXektawpe81mm7lXR+Krza6BbrPCxXaniCckCZTj56r1r4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com; spf=pass smtp.mailfrom=bytedance.com; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b=OTwUzk6L; arc=none smtp.client-ip=209.85.214.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bytedance.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b="OTwUzk6L" Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-1fdd6d81812so34686445ad.1 for ; Sun, 11 Aug 2024 03:19:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1723371586; x=1723976386; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=YeL46knJ+ZqHC6WT5dXT2aP7zuNbEux3//I19LuUM/0=; b=OTwUzk6L5nbjJxLvTk57t2KBqvW1ZODkQks7xyGlW9FJXhnQxFnZVagL4ECEzMFg9U 9/J9GkvjfRQKeLjxV5i2hCMnb3hMMpUrMUpZEalrVwsrRSbYUZTBuFWNaa0T7Xavw+Cj Rt5mCKkx+kNGo6YwZNDAsyWwtUD9+bs1pyjWxqgT5EafV70n4UZFOhB3yrbR1KSRieUx gjI62/b3XrfBFUOLc8bYEnHXoqkQbh166rcWJSpwj10bjHeDbREs6oeMTC9bHLM1p3Za Gf8sFp8OTrmENp+C/mpqIk10P7mrm2iw6tD19hLTqqUbyulnI8xfiMYAr6gqIJguKYUy iaCA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723371586; x=1723976386; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=YeL46knJ+ZqHC6WT5dXT2aP7zuNbEux3//I19LuUM/0=; b=ejMe11tbgZ8+sKAqvEckiLDy1aXm3LJlfoNLfutz/5JvkcXBn4tJTdh9CEtSps2gvD 9UrzNu4XAjT9fXKUxega6h+KcxXgDZKlCoadrLx5th3V5s1muSmeWh8Nnpec8omzCqR5 YuJoOS65N52TdvOnVvGez0t/1uLHnBUiVWmWsia8e77FKQKKgL392Sqn3beC/VGPNS5v ScrULryomeJvghHme8H6Ij9i7UAOWdRFjmHdIz25r8MDxboU/ehsDkP4SAA6n3UKvnTc kfT01T8WtCKcLkfNM4A7AMJSfWFe9RstjTB0ggcpKUWgAu6XR1t8wwaL8tAKXc+LSXS/ hiYA== X-Forwarded-Encrypted: i=1; AJvYcCWuCadEoC0SXMglYM6UzHP98uJKAarTF8RTgtyn3GcExLpGm9oMP2EWX/O4N5Wdms7oCRImsoHEmdyzOd3Jp5QJZxGuH8bUAuzmsCe9 X-Gm-Message-State: AOJu0YwBJ9jQ26E19bveQXRLGnKMNcSpgjJPy+klKsLfERC8Ax+br49A 3KhGbRfa8LIwRx8Su0GUZP6aM/fhg1cTKntpvG2cmsPXHER5R+KX7JmO/sMClg0= X-Google-Smtp-Source: AGHT+IEQlVNibHSTFhZ2s10HfYkPh8ARzQx4wM3RfJLUDq+guzel8WQQIkhmkI9Swx2NtwrIvNj/lw== X-Received: by 2002:a17:902:ced0:b0:1fb:2e9a:beea with SMTP id d9443c01a7336-200ae258491mr73318225ad.0.1723371585843; Sun, 11 Aug 2024 03:19:45 -0700 (PDT) Received: from PXLDJ45XCM.bytedance.net ([139.177.225.239]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-200bbb48b81sm20992155ad.297.2024.08.11.03.19.43 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sun, 11 Aug 2024 03:19:45 -0700 (PDT) From: Muchun Song To: axboe@kernel.dk Cc: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, Muchun Song Subject: [PATCH 4/4] block: fix fix ordering between checking QUEUE_FLAG_QUIESCED and adding requests to hctx->dispatch Date: Sun, 11 Aug 2024 18:19:21 +0800 Message-Id: <20240811101921.4031-5-songmuchun@bytedance.com> X-Mailer: git-send-email 2.39.3 (Apple Git-146) In-Reply-To: <20240811101921.4031-1-songmuchun@bytedance.com> References: <20240811101921.4031-1-songmuchun@bytedance.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Supposing the following scenario. CPU0 CPU1 blk_mq_request_issue_directly() blk_mq_= unquiesce_queue() if (blk_queue_quiesced()) blk= _queue_flag_clear(QUEUE_FLAG_QUIESCED) 3) store blk_mq_insert_request() blk= _mq_run_hw_queues() /* = blk_mq_run_hw_queue() * Add request to dispatch list or set bitmap of = if (!blk_mq_hctx_has_pending()) 4) load * software queue. 1) store = return */ blk_mq_run_hw_queue() if (blk_queue_quiesced()) 2) load return blk_mq_sched_dispatch_requests() The full memory barrier should be inserted between 1) and 2), as well as between 3) and 4) to make sure that either CPU0 sees QUEUE_FLAG_QUIESCED is cleared or CPU1 sees dispatch list or setting of bitmap of software queue. Otherwise, either CPU will not re-run the hardware queue causing starvation. Signed-off-by: Muchun Song --- block/blk-mq.c | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/block/blk-mq.c b/block/blk-mq.c index 385a74e566874..66b21407a9a6c 100644 --- a/block/blk-mq.c +++ b/block/blk-mq.c @@ -264,6 +264,13 @@ void blk_mq_unquiesce_queue(struct request_queue *q) ; } else if (!--q->quiesce_depth) { blk_queue_flag_clear(QUEUE_FLAG_QUIESCED, q); + /** + * The need of memory barrier is in blk_mq_run_hw_queues() to + * make sure clearing of QUEUE_FLAG_QUIESCED is before the + * checking of dispatch list or bitmap of any software queue. + * + * smp_mb__after_atomic(); + */ run_queue =3D true; } spin_unlock_irqrestore(&q->queue_lock, flags); @@ -2222,6 +2229,21 @@ void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx,= bool async) { bool need_run; =20 + /* + * This barrier is used to order adding of dispatch list or setting + * of bitmap of any software queue outside of this function and the + * test of BLK_MQ_S_STOPPED in the following routine. Pairs with the + * barrier in blk_mq_start_stopped_hw_queue(). So dispatch code could + * either see BLK_MQ_S_STOPPED is cleared or dispatch list or setting + * of bitmap of any software queue to avoid missing dispatching + * requests. + * + * This barrier is also used to order adding of dispatch list or + * setting of bitmap of any software queue outside of this function + * and test of QUEUE_FLAG_QUIESCED below. + */ + smp_mb(); + /* * We can't run the queue inline with interrupts disabled. */ @@ -2244,17 +2266,6 @@ void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx,= bool async) if (!need_run) return; =20 - /* - * This barrier is used to order adding of dispatch list or setting - * of bitmap of any software queue outside of this function and the - * test of BLK_MQ_S_STOPPED in the following routine. Pairs with the - * barrier in blk_mq_start_stopped_hw_queue(). So dispatch code could - * either see BLK_MQ_S_STOPPED is cleared or dispatch list or setting - * of bitmap of any software queue to avoid missing dispatching - * requests. - */ - smp_mb(); - if (async || !cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask)) { blk_mq_delay_run_hw_queue(hctx, 0); return; @@ -2308,6 +2319,11 @@ void blk_mq_run_hw_queues(struct request_queue *q, b= ool async) * either see BLK_MQ_S_STOPPED is cleared or dispatch list or setting * of bitmap of any software queue to avoid missing dispatching * requests. + * + * This barrier is also used to order clearing of QUEUE_FLAG_QUIESCED + * outside of this function in blk_mq_unquiesce_queue() and checking + * of dispatch list or bitmap of any software queue in + * blk_mq_run_hw_queue(). */ smp_mb(); =20 --=20 2.20.1