From nobody Fri Nov 29 21:43:21 2024 Received: from mail-pf1-f171.google.com (mail-pf1-f171.google.com [209.85.210.171]) (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 DE366146D45 for ; Sat, 14 Sep 2024 07:30:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726299057; cv=none; b=S0hASRmRNroiZkNyAr0PgkpIf4vWIQcHK9u6dmS+t6vaJAD0iFmG0lLKvyBAymcscRKxHDhw4RC7AUeejUXzo+dNKxy9BhsCoGLmHMtURRUAxogXWiOE0JoXlXITR7sBFYtLcrPIZaX0LPo7qvc889H+wHj3Vur9+8MdBZSojKE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726299057; c=relaxed/simple; bh=NfYRJbO+n9KlpHo78YK1kJWVU0JEHSyMKcCC/4kPv2g=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=NZULghLnmd58vFW5UMSrEsmZqKEU5rlRom8YflXEtoPh/yLIy2bTQf66AHBM2Tvow9TTn6Tg6jn0kN1Aaji3RJMHLCS8k3x/WBPe2GbShou/e0XAc0rTSHNiGFN9FV+5Z17ajklO6GQRy8yZrqoHMmvmWNUs8vzX+KTaJlFEfCs= 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=PB4Qvvdw; arc=none smtp.client-ip=209.85.210.171 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="PB4Qvvdw" Received: by mail-pf1-f171.google.com with SMTP id d2e1a72fcca58-717911ef035so1991850b3a.3 for ; Sat, 14 Sep 2024 00:30:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1726299055; x=1726903855; 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=mqOMUsIf1mnLDxR4ticuxNbonVeXyKT1mpHNVeoepzQ=; b=PB4Qvvdwm51+gOBZprDFm8tkkOYMvdxGtcu/a3L47vXsT/g3hHUPcJEYLh+ZmSEWWJ XTqy1V/u4qeNrXYw3q6/HOxsOROKvgnNhAPV8pCPcjVRnQ7PEYmW18RnfDSKt8uYRcjr Iv0l+yRu9jLmkSRGFAClJHMShW/H6ncFlO8+NgDFbAas+6HyWk12oYDdVkaPiHX4dFNk bA/gtBFo0HkLbvLl208uiYIM/wpnSYoC9CghPGAsZkWz5BmylCEo8Wh4wtJohOT1TJvy VOGzSSkYfZOId6C3JaF5spIk10XNg+FhiXKAMH1fhJoyDT2kdh3jDvKFzVkG+MltYMeC r4tw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726299055; x=1726903855; 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=mqOMUsIf1mnLDxR4ticuxNbonVeXyKT1mpHNVeoepzQ=; b=bpC/Qf/NXXpWukIXaCwqT2XSGGldVGnFAVdeLPOTO7uH3qiGQhx03Ar4gTRxv6CJ4F JJMozIWCWE6M/r430vvGNZfeGYa9diBmfIKZUomZluT/+DeQl+GBTPL80Y8q/Ll8/Elw U7wnup0C9+VT6DLbk2kZ2HPd6jryzv5Z13LexJpTF9qOeWz13f+HMYUmaoO7jWJbsiw8 jheATwAH7O4j6i4RSN+EhnKM/X5wNSd6LlnGDtyryz5tye4Ird7dVcnLRIP3hEUhlsLq ozzI58LEpvgEAsKOfro4Y6BrN0y+fHTKn3YOjGQ5Pfk+l6HdyWGR9Fchmui1Ie6R3WyA 07JA== X-Forwarded-Encrypted: i=1; AJvYcCWv47wp2LfzEquWeO/zu4hL1QLp0qhM5/62Nj5N41t0bO3+igxeyIL2FrTXtUNU9qM8k2XDafNF43Z7PNc=@vger.kernel.org X-Gm-Message-State: AOJu0YxCy/RxyMvhFtiHipZ9PlnilxBmF6lheI/GjXpXL6SplN0gdJgM o5IzL1pA6boESKPmwSzvvIoJDA6pyB2KhLvofhh2kKgMHpjoNHTp6eemtFTQdYY= X-Google-Smtp-Source: AGHT+IHDTQbRO6cV5Mbr0x+fq++p+seJtKKFdtV1cqhsFN5NWwbtCCv5rOqEpU4gF1AdgNhIZhWkdQ== X-Received: by 2002:a05:6a00:993:b0:714:157a:bfc7 with SMTP id d2e1a72fcca58-71926084bb4mr13041594b3a.15.1726299055036; Sat, 14 Sep 2024 00:30:55 -0700 (PDT) Received: from PXLDJ45XCM.bytedance.net ([61.213.176.6]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71944b9ac05sm555687b3a.155.2024.09.14.00.30.51 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sat, 14 Sep 2024 00:30:54 -0700 (PDT) From: Muchun Song To: axboe@kernel.dk, ming.lei@redhat.com Cc: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, muchun.song@linux.dev, Muchun Song , stable@vger.kernel.org Subject: [PATCH v3 1/3] block: fix missing dispatching request when queue is started or unquiesced Date: Sat, 14 Sep 2024 15:28:42 +0800 Message-Id: <20240914072844.18150-2-songmuchun@bytedance.com> X-Mailer: git-send-email 2.39.3 (Apple Git-146) In-Reply-To: <20240914072844.18150-1-songmuchun@bytedance.com> References: <20240914072844.18150-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() virtblk_done() blk_mq_try_issue_directly() if (blk_mq_hctx_stopped()) blk_mq_request_bypass_insert() blk_mq_run_hw_queue() blk_mq_run_hw_queue() blk_mq_run_hw_queue() blk_mq_insert_request() return After CPU0 has marked the queue as stopped, CPU1 will see the queue is stopped. But before CPU1 puts the request on the dispatch list, CPU2 receives the interrupt of completion of request, so it will run the hardware queue and marks the queue as non-stopped. Meanwhile, CPU1 also runs the same hardware queue. After both CPU1 and CPU2 complete blk_mq_run_hw_queue(), CPU1 just puts the request to the same hardware queue and returns. It misses dispatching a request. Fix it by running the hardware queue explicitly. And blk_mq_request_issue_directly() should handle a similar situation. Fix it as well. Fixes: d964f04a8fde ("blk-mq: fix direct issue") Cc: stable@vger.kernel.org Cc: Muchun Song 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 Fri Nov 29 21:43:21 2024 Received: from mail-pf1-f169.google.com (mail-pf1-f169.google.com [209.85.210.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 15F1113A87A for ; Sat, 14 Sep 2024 07:30:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726299060; cv=none; b=Aru8g6ZgN28Q0kK+4k7oR13gODbX8JrrcJ5WiUEEItt7FnD6WbuyzMXTv+RwjTEXtkBsblZP423pc8SyqFvy1IaLAj/ZkYzYksUEbxE4PNVqkvA+RHBbN+WpzXdikmC7aqMVUefuqG2KA+QmlItw0U1qqpYb0nPX+P1y6qSKoCE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726299060; c=relaxed/simple; bh=KPYlAEycxQewUTOUahfTRncxoD2+pw617t+q4Tal4uA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=VrF2KBz1yFYgsIPvDNMSdE2wa3xGUKz1w/sj8DArxOZ1JKj/ShTeJdVIczemcBqKU7vKsjlGye7Oz73OcEri9gB1DefZowQ9j8vNsZiuOLavQn0GNpSewC9Gussj/71J1Pgb5WY0rtB4XA32JAlzrPbpkhktQaHFfXIQIa1KUzo= 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=ROPYQzDg; arc=none smtp.client-ip=209.85.210.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="ROPYQzDg" Received: by mail-pf1-f169.google.com with SMTP id d2e1a72fcca58-718d606726cso1911302b3a.3 for ; Sat, 14 Sep 2024 00:30:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1726299058; x=1726903858; 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=08ooLorU1l3a5zJ7wHu/dTOBgAYH9XEx3vZXzuZBYRU=; b=ROPYQzDgQr5oHOSBnoXaq9/UzDSftmVWVuR6JKwj/zXA8dYrTapQHCRtGHuEeNYDCt /16zmyOk7fI/NEUcQHshR4U7SXRm2k/JsSMDzWpIeHNufqCNBnhTJPz2XV3C9rfIDGKw I0zcQhZ6rn9hqsLagME57Rg/YdxKvKemarvP4kDNl9dlAucB/lrYiosoSkPnfMY2Ye8C ZYT6mF96xIc6/bXkMHdMAyM5yiRyXunOGjJSzhtkfkiC6stnZ5H6HoRXU0j289/XgSsh szVUY2GnW8B5IpZWjmtmQiQWU2Ysya1w6Bvh/D3hL1gNFK40iBI8FivEikDrC1OlkTBM fziw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726299058; x=1726903858; 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=08ooLorU1l3a5zJ7wHu/dTOBgAYH9XEx3vZXzuZBYRU=; b=VPCWWtE+CAZVvOFpT3+s3+kR4tXjVNbHRa2cLAyBHsB0PWvwgB1pcmHUTeF5ASn9XE AIZDq+88bY8mZn7Ltdd1yi8YnVr+/ggLq/u4A4AVmyjXMTEGaZof0Dy38k4yeMzpgGey EbSFcROg2aLkffYYjI2+41uxAfEcStQdDg8iMuWzw+pF/SSem/03RP1/W2zCYACO00QM xx5lxRuMDkhDu9zpdX+pLqfY5ANIyEjsajx0zmvFodOsRQtnalyoAEeFgDoGUN4Dx2n2 J0HnpM2vM7po2m48+KeWTSlLRZc8dod9ajAnGbwUlw/2wjTQh0ODhi6NMgcphH2Jn4E1 Xw/w== X-Forwarded-Encrypted: i=1; AJvYcCVYG/tGtMVOnZTlWEyjbzwJYPP0tfUXPGgbseGyBOf/4oU6gc2xaEfAc1NJw3xkUDh4iVgiveudWAbDONE=@vger.kernel.org X-Gm-Message-State: AOJu0YzS9Tk6TnqYgIaOM5NOVRBQrHANnzBucPmf0wJsw1IJIsrYdCY0 3sFdR1Trgw5O7eYeruexN8ID4pbtrq0Wj3yujcrH5TouGDDfBLgd+4zdKf4l2GP2DfQZ1MR8/JE 9eec= X-Google-Smtp-Source: AGHT+IGZG9wrGWEYM7ouukqw6bRDRDCHTs/ZJP2/izZHnuPe05qp1RIBdiw0AkIVpTS2FuGBrP35Tg== X-Received: by 2002:a05:6a00:1883:b0:706:58ef:613 with SMTP id d2e1a72fcca58-719262066efmr13431606b3a.27.1726299058203; Sat, 14 Sep 2024 00:30:58 -0700 (PDT) Received: from PXLDJ45XCM.bytedance.net ([61.213.176.6]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71944b9ac05sm555687b3a.155.2024.09.14.00.30.55 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sat, 14 Sep 2024 00:30:57 -0700 (PDT) From: Muchun Song To: axboe@kernel.dk, ming.lei@redhat.com Cc: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, muchun.song@linux.dev, Muchun Song , stable@vger.kernel.org Subject: [PATCH v3 2/3] block: fix ordering between checking QUEUE_FLAG_QUIESCED and adding requests Date: Sat, 14 Sep 2024 15:28:43 +0800 Message-Id: <20240914072844.18150-3-songmuchun@bytedance.com> X-Mailer: git-send-email 2.39.3 (Apple Git-146) In-Reply-To: <20240914072844.18150-1-songmuchun@bytedance.com> References: <20240914072844.18150-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_insert_request() 1) store blk_mq_unquiesce_queue() blk_queue_flag_clear() 3) store blk_mq_run_hw_queues() blk_mq_run_hw_queue() if (!blk_mq_hctx_has_pending()) 4) load 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 rerun the hardware queue causing starvation. So the first solution is to 1) add a pair of memory barrier to fix the problem, another solution is to 2) use hctx->queue->queue_lock to synchronize QUEUE_FLAG_QUIESCED. Here, we chose 2) to fix it since memory barrier is not easy to be maintained. Fixes: f4560ffe8cec ("blk-mq: use QUEUE_FLAG_QUIESCED to quiesce queue") Cc: stable@vger.kernel.org Cc: Muchun Song Signed-off-by: Muchun Song Reviewed-by: Ming Lei --- block/blk-mq.c | 47 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/block/blk-mq.c b/block/blk-mq.c index b2d0f22de0c7f..ff6df6c7eeb25 100644 --- a/block/blk-mq.c +++ b/block/blk-mq.c @@ -2202,6 +2202,24 @@ void blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx = *hctx, unsigned long msecs) } EXPORT_SYMBOL(blk_mq_delay_run_hw_queue); =20 +static inline bool blk_mq_hw_queue_need_run(struct blk_mq_hw_ctx *hctx) +{ + bool need_run; + + /* + * When queue is quiesced, we may be switching io scheduler, or + * updating nr_hw_queues, or other things, and we can't run queue + * any more, even blk_mq_hctx_has_pending() can't be called safely. + * + * And queue will be rerun in blk_mq_unquiesce_queue() if it is + * quiesced. + */ + __blk_mq_run_dispatch_ops(hctx->queue, false, + need_run =3D !blk_queue_quiesced(hctx->queue) && + blk_mq_hctx_has_pending(hctx)); + return need_run; +} + /** * blk_mq_run_hw_queue - Start to run a hardware queue. * @hctx: Pointer to the hardware queue to run. @@ -2222,20 +2240,23 @@ void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx= , bool async) =20 might_sleep_if(!async && hctx->flags & BLK_MQ_F_BLOCKING); =20 - /* - * When queue is quiesced, we may be switching io scheduler, or - * updating nr_hw_queues, or other things, and we can't run queue - * any more, even __blk_mq_hctx_has_pending() can't be called safely. - * - * And queue will be rerun in blk_mq_unquiesce_queue() if it is - * quiesced. - */ - __blk_mq_run_dispatch_ops(hctx->queue, false, - need_run =3D !blk_queue_quiesced(hctx->queue) && - blk_mq_hctx_has_pending(hctx)); + need_run =3D blk_mq_hw_queue_need_run(hctx); + if (!need_run) { + unsigned long flags; =20 - if (!need_run) - return; + /* + * Synchronize with blk_mq_unquiesce_queue(), because we check + * if hw queue is quiesced locklessly above, we need the use + * ->queue_lock to make sure we see the up-to-date status to + * not miss rerunning the hw queue. + */ + spin_lock_irqsave(&hctx->queue->queue_lock, flags); + need_run =3D blk_mq_hw_queue_need_run(hctx); + spin_unlock_irqrestore(&hctx->queue->queue_lock, flags); + + if (!need_run) + return; + } =20 if (async || !cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask)) { blk_mq_delay_run_hw_queue(hctx, 0); --=20 2.20.1 From nobody Fri Nov 29 21:43:21 2024 Received: from mail-pg1-f180.google.com (mail-pg1-f180.google.com [209.85.215.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 0C36B13D51B for ; Sat, 14 Sep 2024 07:31:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726299063; cv=none; b=KXoMeCypUI6iEFCYNT/04QX/Fn0QPCspsEIyw58fAxjsPnLqBb7jVFla4n15D52XTxgrhF4j+9Lx+Um/0RVS2TLk4nmTGiGpA1jeGd1tPY4wtXQfi42UxMK3+bIh/0c/a6s5vw9zPpjTN26IFyAyttyLvxy8uq9keHrpVSk0Kdg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726299063; c=relaxed/simple; bh=Nlu7sy6nlj6YleTyQBt/cJY6uYT2Pq2MXEk7kWoy/Mc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=DqSRVbQ5E5y3UK63mO2ilJDB7CG4xImrkRJcmuSPEdHUD7Fy8ysCJMZ1fHvVVtpL0xWvX8GA6LqmcjtHn/ZkngdfrVul1YYJR1MvBTHvul45jO9fun2BuQy0Q7N8lXBdy8/4BJWjP7mSfA6L465LQspLd1otbl2pSQcZqP9TW/M= 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=JF69Bhha; arc=none smtp.client-ip=209.85.215.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="JF69Bhha" Received: by mail-pg1-f180.google.com with SMTP id 41be03b00d2f7-7db238d07b3so2267619a12.2 for ; Sat, 14 Sep 2024 00:31:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1726299061; x=1726903861; 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=wEcd+IiEv1ro72y/n/abcXpVwZU0hZ5zkZIt5HG485g=; b=JF69Bhhas/aaQMM/zk5S9sSa8XHiqpbOBP5yX16Rdhl8rAxwGrVqzw1Y8xNBrkc5E4 ZrPJ+AzghfrHWxSn/YuJEhNENPmbn3GIFha1hGzkJgej+ARh6YylWKgiMeqtXaS5ap+S aqU9cIFUSh/1L2O8lAMYU0/LcLkHRFvGNDWq+rwq5fLOXTYbYwPQ3sVtLR2OUVQK5BA6 UsYnHP+ChzErO+P0pLypgF9GRsh6gHiDwLHPphV8xXDuFnaiLS2LJhE8g4nIOQsmmP5I 22hIKnMWkB00J6JVNBXXS/ioNE9TntE5rIEdwmcWV8BbeDhQsCpjrVNGZoLI0KwbhhHy BYiQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726299061; x=1726903861; 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=wEcd+IiEv1ro72y/n/abcXpVwZU0hZ5zkZIt5HG485g=; b=wIpJDsrnXMLEkxaKxkJmH4VvyWhz6vzlxADNDS2lFOi/uBT5e1FkYAXeJfsa6QX+CF 1qnNWzuVRTlVwvPS4hO24ay+c/xqk4K3PyK7GhzLZIbfa2PPyb4A1UpgIgvbSa8SPzZl W8AuOgJrX50xmDgR37K7b/vTX5ZZrQs8S9/FYO4dGdLIux3aZMKDzBz5XvDLxCH+sfBA WOTO/JLT0WOax4h1ouadQYJXnGE6/WGy2d1HSxlsPf0w+WRaXwuc4eJP++lglCyPVjiL al24TJWMB5BTdH2ONFkLhXIg+2aK787PhSUc9vf45/rkzWCS98hHqVoLKN6rsecYnSbN T8vw== X-Forwarded-Encrypted: i=1; AJvYcCU2xwlsdpiHdrT7mRQx/TzRp1LpuG9fpp8VyTkRPYvUoaZGHCY7P4bIDaxH0+etgyLW94hYHo+3uw1ePMI=@vger.kernel.org X-Gm-Message-State: AOJu0YwVyKwt5EEXqJqrz6oRhLyK3nUO5RSRKDVBwDGZ2h6Eu14A7gga 2i+WAaDYAI0ahjI0e9ll1hF7sc4LleqfKkAx57mtKLtclBLV1veArrTi4fLx9bs= X-Google-Smtp-Source: AGHT+IHm8y07ns7Yps/Sk2jgRKzY5hcTv8FT68ZZZ+L+kPEqfVSn3egGBhElYkjQW1mZy3fYVyID4g== X-Received: by 2002:a05:6a20:c99a:b0:1cf:6ef0:c6b9 with SMTP id adf61e73a8af0-1cf7624b46cmr15117559637.32.1726299061341; Sat, 14 Sep 2024 00:31:01 -0700 (PDT) Received: from PXLDJ45XCM.bytedance.net ([61.213.176.6]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71944b9ac05sm555687b3a.155.2024.09.14.00.30.58 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sat, 14 Sep 2024 00:31:00 -0700 (PDT) From: Muchun Song To: axboe@kernel.dk, ming.lei@redhat.com Cc: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, muchun.song@linux.dev, Muchun Song , stable@vger.kernel.org Subject: [PATCH v3 3/3] block: fix ordering between checking BLK_MQ_S_STOPPED and adding requests Date: Sat, 14 Sep 2024 15:28:44 +0800 Message-Id: <20240914072844.18150-4-songmuchun@bytedance.com> X-Mailer: git-send-email 2.39.3 (Apple Git-146) In-Reply-To: <20240914072844.18150-1-songmuchun@bytedance.com> References: <20240914072844.18150-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 first 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() 1) store blk_mq_start_stopped_hw_queue() clear_bit(BLK_MQ_S_STOPPED) 3) store blk_mq_run_hw_queue() if (!blk_mq_hctx_has_pending()) 4) load return blk_mq_sched_dispatch_requests() blk_mq_run_hw_queue() if (!blk_mq_hctx_has_pending()) return blk_mq_sched_dispatch_requests() if (blk_mq_hctx_stopped()) 2) load return __blk_mq_sched_dispatch_requests() Supposing another scenario. CPU0 CPU1 blk_mq_requeue_work() blk_mq_insert_request() 1) store virtblk_done() blk_mq_start_stopped_hw_queue() blk_mq_run_hw_queues() clear_bit(BLK_MQ_S_STOPPED) 3) store blk_mq_run_hw_queue() if (!blk_mq_hctx_has_pending()) 4) load return blk_mq_sched_dispatch_requests() if (blk_mq_hctx_stopped()) 2) load continue blk_mq_run_hw_queue() Both scenarios are similar, 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 BLK_MQ_S_STOPPED is cleared or CPU1 sees dispatch list. Otherwise, either CPU will not rerun the hardware queue causing starvation of the request. The easy way to fix it is to add the essential full memory barrier into helper of blk_mq_hctx_stopped(). In order to not affect the fast path (hardware queue is not stopped most of the time), we only insert the barrier into the slow path. Actually, only slow path needs to care about missing of dispatching the request to the low-level device driver. Fixes: 320ae51feed5 ("blk-mq: new multi-queue block IO queueing mechanism") Cc: stable@vger.kernel.org Cc: Muchun Song Signed-off-by: Muchun Song Reviewed-by: Ming Lei --- block/blk-mq.c | 6 ++++++ block/blk-mq.h | 13 +++++++++++++ 2 files changed, 19 insertions(+) diff --git a/block/blk-mq.c b/block/blk-mq.c index ff6df6c7eeb25..b90c1680cb780 100644 --- a/block/blk-mq.c +++ b/block/blk-mq.c @@ -2413,6 +2413,12 @@ 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_hctx_stopped() to order the + * clearing of BLK_MQ_S_STOPPED above and the checking of dispatch + * list in the subsequent routine. + */ + smp_mb__after_atomic(); blk_mq_run_hw_queue(hctx, async); } EXPORT_SYMBOL_GPL(blk_mq_start_stopped_hw_queue); diff --git a/block/blk-mq.h b/block/blk-mq.h index 260beea8e332c..f36f3bff70d86 100644 --- a/block/blk-mq.h +++ b/block/blk-mq.h @@ -228,6 +228,19 @@ static inline struct blk_mq_tags *blk_mq_tags_from_dat= a(struct blk_mq_alloc_data =20 static inline bool blk_mq_hctx_stopped(struct blk_mq_hw_ctx *hctx) { + /* Fast path: hardware queue is not stopped most of the time. */ + if (likely(!test_bit(BLK_MQ_S_STOPPED, &hctx->state))) + return false; + + /* + * This barrier is used to order adding of dispatch list before and + * the test of BLK_MQ_S_STOPPED below. Pairs with the memory barrier + * in blk_mq_start_stopped_hw_queue() so that dispatch code could + * either see BLK_MQ_S_STOPPED is cleared or dispatch list is not + * empty to avoid missing dispatching requests. + */ + smp_mb(); + return test_bit(BLK_MQ_S_STOPPED, &hctx->state); } =20 --=20 2.20.1