From nobody Sun Oct 5 20:01:09 2025 Received: from dggsgout12.his.huawei.com (dggsgout12.his.huawei.com [45.249.212.56]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 15F59292B45; Wed, 30 Jul 2025 08:29:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.56 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753864145; cv=none; b=JcZuD14b8AZGDUu4IAg/cKJZWu+D3DM+6efDyzykUX8NgSDyNp/mYidNZAi/Gdno/+Hm7mUnlMLeuYzHZOsWAhKCwmBxCyLbNKsKcn6ecBrEzxi5+uyXj/qdfbM+z7VRn4gqHl9vZOLFMAmLgTPeJpWvpl5uPrQIlJx0PsDf+CY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753864145; c=relaxed/simple; bh=R9mFR35pKpTZrPbmRbP2nm/mOvzrvfpl0dmwwuPTIe4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=a/m/8ZxX1Sa9jQOkUXbWzRKaCpQK4kwba6NHV5heSZWvNEWd+dxGOoirGE5iqqXmEU9YrnTM53gyuN2MIOLEEZggydjPx6oeWYLFPfjrI0ZPoWyQbpS4xlR/sb31bLPAdS4Kj0vdV1cQqMqTe0tEFBpYKyQRmyxJ3Fsnln0UZ2k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com; spf=none smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=45.249.212.56 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.19.163.216]) by dggsgout12.his.huawei.com (SkyGuard) with ESMTPS id 4bsQNX5fWGzKHMyR; Wed, 30 Jul 2025 16:29:00 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.128]) by mail.maildlp.com (Postfix) with ESMTP id A94C11A111D; Wed, 30 Jul 2025 16:28:59 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.104.67]) by APP4 (Coremail) with SMTP id gCh0CgAXkxPI14lokcLlBw--.17899S5; Wed, 30 Jul 2025 16:28:59 +0800 (CST) From: Yu Kuai To: dlemoal@kernel.org, hare@suse.de, jack@suse.cz, tj@kernel.org, josef@toxicpanda.com, axboe@kernel.dk, yukuai3@huawei.com Cc: cgroups@vger.kernel.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, yukuai1@huaweicloud.com, yi.zhang@huawei.com, yangerkun@huawei.com, johnny.chenyi@huawei.com Subject: [PATCH v2 1/5] blk-mq-sched: introduce high level elevator lock Date: Wed, 30 Jul 2025 16:22:03 +0800 Message-Id: <20250730082207.4031744-2-yukuai1@huaweicloud.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20250730082207.4031744-1-yukuai1@huaweicloud.com> References: <20250730082207.4031744-1-yukuai1@huaweicloud.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 X-CM-TRANSID: gCh0CgAXkxPI14lokcLlBw--.17899S5 X-Coremail-Antispam: 1UD129KBjvJXoWxXw1UJw47Gr1fGr1kCr1kGrg_yoW5Xr1DpF 4rJanIkFs0qF10vF97Aa13Xw13J3929r9xW398A3yayFn7GrZ3XF1kGF1UZFWIyF4fCFsr Wr1ktaykZa42g3DanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUm014x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_Jr4l82xGYIkIc2 x26xkF7I0E14v26r4j6ryUM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2z4x0 Y4vE2Ix0cI8IcVAFwI0_tr0E3s1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr1j6F4UJw A2z4x0Y4vEx4A2jsIE14v26rxl6s0DM28EF7xvwVC2z280aVCY1x0267AKxVW0oVCq3wAS 0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7IYx2 IY67AKxVWUJVWUGwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6xCaFVCjc4AY6r1j6r4UM4x0 Y48IcxkI7VAKI48JM4x0x7Aq67IIx4CEVc8vx2IErcIFxwACI402YVCY1x02628vn2kIc2 xKxwCY1x0262kKe7AKxVWUtVW8ZwCF04k20xvY0x0EwIxGrwCFx2IqxVCFs4IE7xkEbVWU JVW8JwC20s026c02F40E14v26r1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E67AF67 kF1VAFwI0_Jw0_GFylIxkGc2Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVWUJVWUCwCI42IY 6xIIjxv20xvEc7CjxVAFwI0_Gr0_Cr1lIxAIcVCF04k26cxKx2IYs7xG6r1j6r1xMIIF0x vEx4A2jsIE14v26r1j6r4UMIIF0xvEx4A2jsIEc7CjxVAFwI0_Gr0_Gr1UYxBIdaVFxhVj vjDU0xZFpf9x0JU4OJ5UUUUU= X-CM-SenderInfo: 51xn3trlr6x35dzhxuhorxvhhfrp/ Content-Type: text/plain; charset="utf-8" From: Yu Kuai Currently, both mq-deadline and bfq have global spin lock that will be grabbed inside elevator methods like dispatch_request, insert_requests, and bio_merge. And the global lock is the main reason mq-deadline and bfq can't scale very well. While dispatching request, blk_mq_get_disatpch_budget() and blk_mq_get_driver_tag() must be called, and they are not ready to be called inside elevator methods, hence introduce a new method like dispatch_requests is not possible. Hence introduce a new high level elevator lock, currently it is protecting dispatch_request only. Following patches will convert mq-deadline and bfq to use this lock and finally support request batch dispatching by calling the method multiple time while holding the lock. Signed-off-by: Yu Kuai Reviewed-by: Hannes Reinecke --- block/blk-mq-sched.c | 6 ++++++ block/elevator.c | 1 + block/elevator.h | 4 ++-- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/block/blk-mq-sched.c b/block/blk-mq-sched.c index 55a0fd105147..7911fae75ce4 100644 --- a/block/blk-mq-sched.c +++ b/block/blk-mq-sched.c @@ -98,6 +98,7 @@ static int __blk_mq_do_dispatch_sched(struct blk_mq_hw_ct= x *hctx) max_dispatch =3D hctx->queue->nr_requests; =20 do { + bool sq_sched =3D blk_queue_sq_sched(q); struct request *rq; int budget_token; =20 @@ -113,7 +114,12 @@ static int __blk_mq_do_dispatch_sched(struct blk_mq_hw= _ctx *hctx) if (budget_token < 0) break; =20 + if (sq_sched) + spin_lock(&e->lock); rq =3D e->type->ops.dispatch_request(hctx); + if (sq_sched) + spin_unlock(&e->lock); + if (!rq) { blk_mq_put_dispatch_budget(q, budget_token); /* diff --git a/block/elevator.c b/block/elevator.c index 88f8f36bed98..45303af0ca73 100644 --- a/block/elevator.c +++ b/block/elevator.c @@ -144,6 +144,7 @@ struct elevator_queue *elevator_alloc(struct request_qu= eue *q, eq->type =3D e; kobject_init(&eq->kobj, &elv_ktype); mutex_init(&eq->sysfs_lock); + spin_lock_init(&eq->lock); hash_init(eq->hash); =20 return eq; diff --git a/block/elevator.h b/block/elevator.h index a07ce773a38f..cbbac4f7825c 100644 --- a/block/elevator.h +++ b/block/elevator.h @@ -110,12 +110,12 @@ struct request *elv_rqhash_find(struct request_queue = *q, sector_t offset); /* * each queue has an elevator_queue associated with it */ -struct elevator_queue -{ +struct elevator_queue { struct elevator_type *type; void *elevator_data; struct kobject kobj; struct mutex sysfs_lock; + spinlock_t lock; unsigned long flags; DECLARE_HASHTABLE(hash, ELV_HASH_BITS); }; --=20 2.39.2 From nobody Sun Oct 5 20:01:09 2025 Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1252928FFDE; Wed, 30 Jul 2025 08:29:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753864146; cv=none; b=UxyRGWPM3AOPckulgBu1IY+VPeOeiC74vPVG0ONmkFmfUh5EitzMQ7wb6Cr21IWCQev176ywjbPvgVOXRsN8JqwUL/9pewC5jUQiKpvxFK77D35bYFvuc7UZdaItWxhef7AxtE68k0H9nTmlvNpvuO4OZVEaUEcNRImSQp31shM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753864146; c=relaxed/simple; bh=LNoyevvltRrdHXVY35kHZMJt8kx61bjybqN6yNqlbsM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ghpxuK+DzS86km7a8odk2lTKuVHi9H35W61gQr8lt+IZjgfxOqZ7qADBRqjvHQelLN+I+hZc8/FiCYlSSglHU+Pp6Bo/5+JUB9p1WNN9PCsFcnXxb+Avu8vdYnG2Bxuq9HX0gUw2a54BgRpfcD20ly1kRinttCtYw2cWDFRw7i0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com; spf=pass smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=45.249.212.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.19.163.216]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTPS id 4bsQNY4Sn6zYQvKg; Wed, 30 Jul 2025 16:29:01 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.128]) by mail.maildlp.com (Postfix) with ESMTP id 4C5E21A111D; Wed, 30 Jul 2025 16:29:00 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.104.67]) by APP4 (Coremail) with SMTP id gCh0CgAXkxPI14lokcLlBw--.17899S6; Wed, 30 Jul 2025 16:29:00 +0800 (CST) From: Yu Kuai To: dlemoal@kernel.org, hare@suse.de, jack@suse.cz, tj@kernel.org, josef@toxicpanda.com, axboe@kernel.dk, yukuai3@huawei.com Cc: cgroups@vger.kernel.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, yukuai1@huaweicloud.com, yi.zhang@huawei.com, yangerkun@huawei.com, johnny.chenyi@huawei.com Subject: [PATCH v2 2/5] mq-deadline: switch to use elevator lock Date: Wed, 30 Jul 2025 16:22:04 +0800 Message-Id: <20250730082207.4031744-3-yukuai1@huaweicloud.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20250730082207.4031744-1-yukuai1@huaweicloud.com> References: <20250730082207.4031744-1-yukuai1@huaweicloud.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 X-CM-TRANSID: gCh0CgAXkxPI14lokcLlBw--.17899S6 X-Coremail-Antispam: 1UD129KBjvJXoW3XryrGFyfKFWrZFW8ZF18Zrb_yoW3AF1rpF W5Ka1Yyr4rJFsrXF1DJayDZr4Sgw4Iv347tryfGw4fKF1qyr9rXF1UGF1Fvrs3JF97CFsI gF4qqa98JF17JwUanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUm014x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_Jryl82xGYIkIc2 x26xkF7I0E14v26ryj6s0DM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2z4x0 Y4vE2Ix0cI8IcVAFwI0_tr0E3s1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr1j6F4UJw A2z4x0Y4vEx4A2jsIE14v26rxl6s0DM28EF7xvwVC2z280aVCY1x0267AKxVW0oVCq3wAS 0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7IYx2 IY67AKxVWUJVWUGwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6xCaFVCjc4AY6r1j6r4UM4x0 Y48IcxkI7VAKI48JM4x0x7Aq67IIx4CEVc8vx2IErcIFxwACI402YVCY1x02628vn2kIc2 xKxwCY1x0262kKe7AKxVWUtVW8ZwCF04k20xvY0x0EwIxGrwCFx2IqxVCFs4IE7xkEbVWU JVW8JwC20s026c02F40E14v26r1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E67AF67 kF1VAFwI0_Jw0_GFylIxkGc2Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVWUJVWUCwCI42IY 6xIIjxv20xvEc7CjxVAFwI0_Gr0_Cr1lIxAIcVCF04k26cxKx2IYs7xG6r1j6r1xMIIF0x vEx4A2jsIE14v26r1j6r4UMIIF0xvEx4A2jsIEc7CjxVAFwI0_Gr0_Gr1UYxBIdaVFxhVj vjDU0xZFpf9x0JUQXo7UUUUU= X-CM-SenderInfo: 51xn3trlr6x35dzhxuhorxvhhfrp/ Content-Type: text/plain; charset="utf-8" From: Yu Kuai Replace the internal spinlock 'dd->lock' with the new spinlock in elevator_queue, there are no functional changes. Signed-off-by: Yu Kuai --- block/mq-deadline.c | 58 +++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 31 deletions(-) diff --git a/block/mq-deadline.c b/block/mq-deadline.c index 9ab6c6256695..2054c023e855 100644 --- a/block/mq-deadline.c +++ b/block/mq-deadline.c @@ -101,7 +101,7 @@ struct deadline_data { u32 async_depth; int prio_aging_expire; =20 - spinlock_t lock; + spinlock_t *lock; }; =20 /* Maps an I/O priority class to a deadline scheduler priority. */ @@ -213,7 +213,7 @@ static void dd_merged_requests(struct request_queue *q,= struct request *req, const u8 ioprio_class =3D dd_rq_ioclass(next); const enum dd_prio prio =3D ioprio_class_to_prio[ioprio_class]; =20 - lockdep_assert_held(&dd->lock); + lockdep_assert_held(dd->lock); =20 dd->per_prio[prio].stats.merged++; =20 @@ -253,7 +253,7 @@ static u32 dd_queued(struct deadline_data *dd, enum dd_= prio prio) { const struct io_stats_per_prio *stats =3D &dd->per_prio[prio].stats; =20 - lockdep_assert_held(&dd->lock); + lockdep_assert_held(dd->lock); =20 return stats->inserted - atomic_read(&stats->completed); } @@ -323,7 +323,7 @@ static struct request *__dd_dispatch_request(struct dea= dline_data *dd, enum dd_prio prio; u8 ioprio_class; =20 - lockdep_assert_held(&dd->lock); + lockdep_assert_held(dd->lock); =20 if (!list_empty(&per_prio->dispatch)) { rq =3D list_first_entry(&per_prio->dispatch, struct request, @@ -434,7 +434,7 @@ static struct request *dd_dispatch_prio_aged_requests(s= truct deadline_data *dd, enum dd_prio prio; int prio_cnt; =20 - lockdep_assert_held(&dd->lock); + lockdep_assert_held(dd->lock); =20 prio_cnt =3D !!dd_queued(dd, DD_RT_PRIO) + !!dd_queued(dd, DD_BE_PRIO) + !!dd_queued(dd, DD_IDLE_PRIO); @@ -466,10 +466,9 @@ static struct request *dd_dispatch_request(struct blk_= mq_hw_ctx *hctx) struct request *rq; enum dd_prio prio; =20 - spin_lock(&dd->lock); rq =3D dd_dispatch_prio_aged_requests(dd, now); if (rq) - goto unlock; + return rq; =20 /* * Next, dispatch requests in priority order. Ignore lower priority @@ -481,9 +480,6 @@ static struct request *dd_dispatch_request(struct blk_m= q_hw_ctx *hctx) break; } =20 -unlock: - spin_unlock(&dd->lock); - return rq; } =20 @@ -538,9 +534,9 @@ static void dd_exit_sched(struct elevator_queue *e) WARN_ON_ONCE(!list_empty(&per_prio->fifo_list[DD_READ])); WARN_ON_ONCE(!list_empty(&per_prio->fifo_list[DD_WRITE])); =20 - spin_lock(&dd->lock); + spin_lock(dd->lock); queued =3D dd_queued(dd, prio); - spin_unlock(&dd->lock); + spin_unlock(dd->lock); =20 WARN_ONCE(queued !=3D 0, "statistics for priority %d: i %u m %u d %u c %u\n", @@ -587,7 +583,7 @@ static int dd_init_sched(struct request_queue *q, struc= t elevator_type *e) dd->last_dir =3D DD_WRITE; dd->fifo_batch =3D fifo_batch; dd->prio_aging_expire =3D prio_aging_expire; - spin_lock_init(&dd->lock); + dd->lock =3D &eq->lock; =20 /* We dispatch from request queue wide instead of hw queue */ blk_queue_flag_set(QUEUE_FLAG_SQ_SCHED, q); @@ -643,9 +639,9 @@ static bool dd_bio_merge(struct request_queue *q, struc= t bio *bio, struct request *free =3D NULL; bool ret; =20 - spin_lock(&dd->lock); + spin_lock(dd->lock); ret =3D blk_mq_sched_try_merge(q, bio, nr_segs, &free); - spin_unlock(&dd->lock); + spin_unlock(dd->lock); =20 if (free) blk_mq_free_request(free); @@ -667,7 +663,7 @@ static void dd_insert_request(struct blk_mq_hw_ctx *hct= x, struct request *rq, struct dd_per_prio *per_prio; enum dd_prio prio; =20 - lockdep_assert_held(&dd->lock); + lockdep_assert_held(dd->lock); =20 prio =3D ioprio_class_to_prio[ioprio_class]; per_prio =3D &dd->per_prio[prio]; @@ -711,7 +707,7 @@ static void dd_insert_requests(struct blk_mq_hw_ctx *hc= tx, struct deadline_data *dd =3D q->elevator->elevator_data; LIST_HEAD(free); =20 - spin_lock(&dd->lock); + spin_lock(dd->lock); while (!list_empty(list)) { struct request *rq; =20 @@ -719,7 +715,7 @@ static void dd_insert_requests(struct blk_mq_hw_ctx *hc= tx, list_del_init(&rq->queuelist); dd_insert_request(hctx, rq, flags, &free); } - spin_unlock(&dd->lock); + spin_unlock(dd->lock); =20 blk_mq_free_requests(&free); } @@ -835,13 +831,13 @@ static const struct elv_fs_entry deadline_attrs[] =3D= { #define DEADLINE_DEBUGFS_DDIR_ATTRS(prio, data_dir, name) \ static void *deadline_##name##_fifo_start(struct seq_file *m, \ loff_t *pos) \ - __acquires(&dd->lock) \ + __acquires(dd->lock) \ { \ struct request_queue *q =3D m->private; \ struct deadline_data *dd =3D q->elevator->elevator_data; \ struct dd_per_prio *per_prio =3D &dd->per_prio[prio]; \ \ - spin_lock(&dd->lock); \ + spin_lock(dd->lock); \ return seq_list_start(&per_prio->fifo_list[data_dir], *pos); \ } \ \ @@ -856,12 +852,12 @@ static void *deadline_##name##_fifo_next(struct seq_f= ile *m, void *v, \ } \ \ static void deadline_##name##_fifo_stop(struct seq_file *m, void *v) \ - __releases(&dd->lock) \ + __releases(dd->lock) \ { \ struct request_queue *q =3D m->private; \ struct deadline_data *dd =3D q->elevator->elevator_data; \ \ - spin_unlock(&dd->lock); \ + spin_unlock(dd->lock); \ } \ \ static const struct seq_operations deadline_##name##_fifo_seq_ops =3D { \ @@ -927,11 +923,11 @@ static int dd_queued_show(void *data, struct seq_file= *m) struct deadline_data *dd =3D q->elevator->elevator_data; u32 rt, be, idle; =20 - spin_lock(&dd->lock); + spin_lock(dd->lock); rt =3D dd_queued(dd, DD_RT_PRIO); be =3D dd_queued(dd, DD_BE_PRIO); idle =3D dd_queued(dd, DD_IDLE_PRIO); - spin_unlock(&dd->lock); + spin_unlock(dd->lock); =20 seq_printf(m, "%u %u %u\n", rt, be, idle); =20 @@ -943,7 +939,7 @@ static u32 dd_owned_by_driver(struct deadline_data *dd,= enum dd_prio prio) { const struct io_stats_per_prio *stats =3D &dd->per_prio[prio].stats; =20 - lockdep_assert_held(&dd->lock); + lockdep_assert_held(dd->lock); =20 return stats->dispatched + stats->merged - atomic_read(&stats->completed); @@ -955,11 +951,11 @@ static int dd_owned_by_driver_show(void *data, struct= seq_file *m) struct deadline_data *dd =3D q->elevator->elevator_data; u32 rt, be, idle; =20 - spin_lock(&dd->lock); + spin_lock(dd->lock); rt =3D dd_owned_by_driver(dd, DD_RT_PRIO); be =3D dd_owned_by_driver(dd, DD_BE_PRIO); idle =3D dd_owned_by_driver(dd, DD_IDLE_PRIO); - spin_unlock(&dd->lock); + spin_unlock(dd->lock); =20 seq_printf(m, "%u %u %u\n", rt, be, idle); =20 @@ -969,13 +965,13 @@ static int dd_owned_by_driver_show(void *data, struct= seq_file *m) #define DEADLINE_DISPATCH_ATTR(prio) \ static void *deadline_dispatch##prio##_start(struct seq_file *m, \ loff_t *pos) \ - __acquires(&dd->lock) \ + __acquires(dd->lock) \ { \ struct request_queue *q =3D m->private; \ struct deadline_data *dd =3D q->elevator->elevator_data; \ struct dd_per_prio *per_prio =3D &dd->per_prio[prio]; \ \ - spin_lock(&dd->lock); \ + spin_lock(dd->lock); \ return seq_list_start(&per_prio->dispatch, *pos); \ } \ \ @@ -990,12 +986,12 @@ static void *deadline_dispatch##prio##_next(struct se= q_file *m, \ } \ \ static void deadline_dispatch##prio##_stop(struct seq_file *m, void *v) \ - __releases(&dd->lock) \ + __releases(dd->lock) \ { \ struct request_queue *q =3D m->private; \ struct deadline_data *dd =3D q->elevator->elevator_data; \ \ - spin_unlock(&dd->lock); \ + spin_unlock(dd->lock); \ } \ \ static const struct seq_operations deadline_dispatch##prio##_seq_ops =3D {= \ --=20 2.39.2 From nobody Sun Oct 5 20:01:09 2025 Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 34EA0292B3F; Wed, 30 Jul 2025 08:29:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753864146; cv=none; b=A141yAbnsKHFihxLieuZLE2zOec/GYeFbbvM2YjKQWR/oHsiyEmlpGCrUXCVOvRsjULT5cZoZqiiO/6y60f7f4ECHUFF/iEolj8IhNPAasC9qZcFZnxlZfbM4zOJAcoxlhOq2WmVq16NkWWUk8DwlBuCplHqGpIGd+CmAtEdFBk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753864146; c=relaxed/simple; bh=060EPLanmToRXHpTu+odSf1DZ0wAtMDdgwVIJQiK8wk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=XkyABtFD/uyYgh8m/hnVluy9C3Su6lNYljeAzEoUqH82ZSP7e0eB2h194Uync7UjuzXmfNTqFHHQTpPHaRo0lvXmVgx6yClJ3HBt6s0BphwNLDv+BqG/Lpsp41GRU6AqwEpF0U/I+clrEZSnLsjiDunXBFsOGL2aYAeWIRMwgEc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com; spf=pass smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=45.249.212.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.19.93.142]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTPS id 4bsQNZ1bgszYQvLL; Wed, 30 Jul 2025 16:29:02 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.128]) by mail.maildlp.com (Postfix) with ESMTP id DA31D1A134A; Wed, 30 Jul 2025 16:29:00 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.104.67]) by APP4 (Coremail) with SMTP id gCh0CgAXkxPI14lokcLlBw--.17899S7; Wed, 30 Jul 2025 16:29:00 +0800 (CST) From: Yu Kuai To: dlemoal@kernel.org, hare@suse.de, jack@suse.cz, tj@kernel.org, josef@toxicpanda.com, axboe@kernel.dk, yukuai3@huawei.com Cc: cgroups@vger.kernel.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, yukuai1@huaweicloud.com, yi.zhang@huawei.com, yangerkun@huawei.com, johnny.chenyi@huawei.com Subject: [PATCH v2 3/5] block, bfq: switch to use elevator lock Date: Wed, 30 Jul 2025 16:22:05 +0800 Message-Id: <20250730082207.4031744-4-yukuai1@huaweicloud.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20250730082207.4031744-1-yukuai1@huaweicloud.com> References: <20250730082207.4031744-1-yukuai1@huaweicloud.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 X-CM-TRANSID: gCh0CgAXkxPI14lokcLlBw--.17899S7 X-Coremail-Antispam: 1UD129KBjvJXoW3Wry5uryruFWkJFW5AFyxKrg_yoWfZF4fpa 1fKF4ayw48XF10qF4DGw4qqr47Kws5ur9rKryfX3yftFWrt3sxXF1FyryFvF4SqFn7Crsx Wr1jq39YyF1jyaDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUmF14x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_JrWl82xGYIkIc2 x26xkF7I0E14v26ryj6s0DM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2z4x0 Y4vE2Ix0cI8IcVAFwI0_tr0E3s1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr1j6F4UJw A2z4x0Y4vEx4A2jsIE14v26rxl6s0DM28EF7xvwVC2z280aVCY1x0267AKxVW0oVCq3wAS 0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7IYx2 IY67AKxVWUJVWUGwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6xCaFVCjc4AY6r1j6r4UM4x0 Y48IcxkI7VAKI48JM4x0x7Aq67IIx4CEVc8vx2IErcIFxwACI402YVCY1x02628vn2kIc2 xKxwCY1x0262kKe7AKxVWUtVW8ZwCF04k20xvY0x0EwIxGrwCFx2IqxVCFs4IE7xkEbVWU JVW8JwC20s026c02F40E14v26r1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E67AF67 kF1VAFwI0_Jw0_GFylIxkGc2Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVWUJVWUCwCI42IY 6xIIjxv20xvEc7CjxVAFwI0_Cr0_Gr1UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42 IY6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv6xkF7I0E14v26r4UJVWxJrUvcSsGvfC2 KfnxnUUI43ZEXa7VUbpwZ7UUUUU== X-CM-SenderInfo: 51xn3trlr6x35dzhxuhorxvhhfrp/ Content-Type: text/plain; charset="utf-8" From: Yu Kuai Replace the internal spinlock bfqd->lock with the new spinlock in elevator_queue. There are no functional changes. Signed-off-by: Yu Kuai Reviewed-by: Hannes Reinecke lock, flags); + spin_lock_irqsave(bfqd->lock, flags); =20 if (!entity) /* root group */ goto put_async_queues; @@ -923,7 +923,7 @@ static void bfq_pd_offline(struct blkg_policy_data *pd) put_async_queues: bfq_put_async_queues(bfqd, bfqg); =20 - spin_unlock_irqrestore(&bfqd->lock, flags); + spin_unlock_irqrestore(bfqd->lock, flags); /* * @blkg is going offline and will be ignored by * blkg_[rw]stat_recursive_sum(). Transfer stats to the parent so diff --git a/block/bfq-iosched.c b/block/bfq-iosched.c index a6a574a8eac9..1b4e01d15cfe 100644 --- a/block/bfq-iosched.c +++ b/block/bfq-iosched.c @@ -466,7 +466,7 @@ static struct bfq_io_cq *bfq_bic_lookup(struct request_= queue *q) */ void bfq_schedule_dispatch(struct bfq_data *bfqd) { - lockdep_assert_held(&bfqd->lock); + lockdep_assert_held(bfqd->lock); =20 if (bfqd->queued !=3D 0) { bfq_log(bfqd, "schedule dispatch"); @@ -591,7 +591,7 @@ static bool bfqq_request_over_limit(struct bfq_data *bf= qd, int level; =20 retry: - spin_lock_irq(&bfqd->lock); + spin_lock_irq(bfqd->lock); bfqq =3D bic_to_bfqq(bic, op_is_sync(opf), act_idx); if (!bfqq) goto out; @@ -603,7 +603,7 @@ static bool bfqq_request_over_limit(struct bfq_data *bf= qd, /* +1 for bfqq entity, root cgroup not included */ depth =3D bfqg_to_blkg(bfqq_group(bfqq))->blkcg->css.cgroup->level + 1; if (depth > alloc_depth) { - spin_unlock_irq(&bfqd->lock); + spin_unlock_irq(bfqd->lock); if (entities !=3D inline_entities) kfree(entities); entities =3D kmalloc_array(depth, sizeof(*entities), GFP_NOIO); @@ -661,7 +661,7 @@ static bool bfqq_request_over_limit(struct bfq_data *bf= qd, } } out: - spin_unlock_irq(&bfqd->lock); + spin_unlock_irq(bfqd->lock); if (entities !=3D inline_entities) kfree(entities); return ret; @@ -2452,7 +2452,7 @@ static bool bfq_bio_merge(struct request_queue *q, st= ruct bio *bio, struct request *free =3D NULL; bool ret; =20 - spin_lock_irq(&bfqd->lock); + spin_lock_irq(bfqd->lock); =20 if (bic) { /* @@ -2470,7 +2470,7 @@ static bool bfq_bio_merge(struct request_queue *q, st= ruct bio *bio, =20 ret =3D blk_mq_sched_try_merge(q, bio, nr_segs, &free); =20 - spin_unlock_irq(&bfqd->lock); + spin_unlock_irq(bfqd->lock); if (free) blk_mq_free_request(free); =20 @@ -2645,7 +2645,7 @@ static void bfq_end_wr(struct bfq_data *bfqd) struct bfq_queue *bfqq; int i; =20 - spin_lock_irq(&bfqd->lock); + spin_lock_irq(bfqd->lock); =20 for (i =3D 0; i < bfqd->num_actuators; i++) { list_for_each_entry(bfqq, &bfqd->active_list[i], bfqq_list) @@ -2655,7 +2655,7 @@ static void bfq_end_wr(struct bfq_data *bfqd) bfq_bfqq_end_wr(bfqq); bfq_end_wr_async(bfqd); =20 - spin_unlock_irq(&bfqd->lock); + spin_unlock_irq(bfqd->lock); } =20 static sector_t bfq_io_struct_pos(void *io_struct, bool request) @@ -5301,8 +5301,6 @@ static struct request *bfq_dispatch_request(struct bl= k_mq_hw_ctx *hctx) struct bfq_queue *in_serv_queue; bool waiting_rq, idle_timer_disabled =3D false; =20 - spin_lock_irq(&bfqd->lock); - in_serv_queue =3D bfqd->in_service_queue; waiting_rq =3D in_serv_queue && bfq_bfqq_wait_request(in_serv_queue); =20 @@ -5312,7 +5310,6 @@ static struct request *bfq_dispatch_request(struct bl= k_mq_hw_ctx *hctx) waiting_rq && !bfq_bfqq_wait_request(in_serv_queue); } =20 - spin_unlock_irq(&bfqd->lock); bfq_update_dispatch_stats(hctx->queue, rq, idle_timer_disabled ? in_serv_queue : NULL, idle_timer_disabled); @@ -5490,9 +5487,9 @@ static void bfq_exit_icq(struct io_cq *icq) * this is the last time these queues are accessed. */ if (bfqd) { - spin_lock_irqsave(&bfqd->lock, flags); + spin_lock_irqsave(bfqd->lock, flags); _bfq_exit_icq(bic, bfqd->num_actuators); - spin_unlock_irqrestore(&bfqd->lock, flags); + spin_unlock_irqrestore(bfqd->lock, flags); } else { _bfq_exit_icq(bic, BFQ_MAX_ACTUATORS); } @@ -6248,10 +6245,10 @@ static void bfq_insert_request(struct blk_mq_hw_ctx= *hctx, struct request *rq, if (!cgroup_subsys_on_dfl(io_cgrp_subsys) && rq->bio) bfqg_stats_update_legacy_io(q, rq); #endif - spin_lock_irq(&bfqd->lock); + spin_lock_irq(bfqd->lock); bfqq =3D bfq_init_rq(rq); if (blk_mq_sched_try_insert_merge(q, rq, &free)) { - spin_unlock_irq(&bfqd->lock); + spin_unlock_irq(bfqd->lock); blk_mq_free_requests(&free); return; } @@ -6284,7 +6281,7 @@ static void bfq_insert_request(struct blk_mq_hw_ctx *= hctx, struct request *rq, * merge). */ cmd_flags =3D rq->cmd_flags; - spin_unlock_irq(&bfqd->lock); + spin_unlock_irq(bfqd->lock); =20 bfq_update_insert_stats(q, bfqq, idle_timer_disabled, cmd_flags); @@ -6665,7 +6662,7 @@ static void bfq_finish_requeue_request(struct request= *rq) rq->io_start_time_ns, rq->cmd_flags); =20 - spin_lock_irqsave(&bfqd->lock, flags); + spin_lock_irqsave(bfqd->lock, flags); if (likely(rq->rq_flags & RQF_STARTED)) { if (rq =3D=3D bfqd->waited_rq) bfq_update_inject_limit(bfqd, bfqq); @@ -6675,7 +6672,7 @@ static void bfq_finish_requeue_request(struct request= *rq) bfqq_request_freed(bfqq); bfq_put_queue(bfqq); RQ_BIC(rq)->requests--; - spin_unlock_irqrestore(&bfqd->lock, flags); + spin_unlock_irqrestore(bfqd->lock, flags); =20 /* * Reset private fields. In case of a requeue, this allows @@ -7006,7 +7003,7 @@ bfq_idle_slice_timer_body(struct bfq_data *bfqd, stru= ct bfq_queue *bfqq) enum bfqq_expiration reason; unsigned long flags; =20 - spin_lock_irqsave(&bfqd->lock, flags); + spin_lock_irqsave(bfqd->lock, flags); =20 /* * Considering that bfqq may be in race, we should firstly check @@ -7016,7 +7013,7 @@ bfq_idle_slice_timer_body(struct bfq_data *bfqd, stru= ct bfq_queue *bfqq) * been cleared in __bfq_bfqd_reset_in_service func. */ if (bfqq !=3D bfqd->in_service_queue) { - spin_unlock_irqrestore(&bfqd->lock, flags); + spin_unlock_irqrestore(bfqd->lock, flags); return; } =20 @@ -7044,7 +7041,7 @@ bfq_idle_slice_timer_body(struct bfq_data *bfqd, stru= ct bfq_queue *bfqq) =20 schedule_dispatch: bfq_schedule_dispatch(bfqd); - spin_unlock_irqrestore(&bfqd->lock, flags); + spin_unlock_irqrestore(bfqd->lock, flags); } =20 /* @@ -7169,10 +7166,10 @@ static void bfq_exit_queue(struct elevator_queue *e) =20 hrtimer_cancel(&bfqd->idle_slice_timer); =20 - spin_lock_irq(&bfqd->lock); + spin_lock_irq(bfqd->lock); list_for_each_entry_safe(bfqq, n, &bfqd->idle_list, bfqq_list) bfq_deactivate_bfqq(bfqd, bfqq, false, false); - spin_unlock_irq(&bfqd->lock); + spin_unlock_irq(bfqd->lock); =20 for (actuator =3D 0; actuator < bfqd->num_actuators; actuator++) WARN_ON_ONCE(bfqd->rq_in_driver[actuator]); @@ -7186,10 +7183,10 @@ static void bfq_exit_queue(struct elevator_queue *e) #ifdef CONFIG_BFQ_GROUP_IOSCHED blkcg_deactivate_policy(bfqd->queue->disk, &blkcg_policy_bfq); #else - spin_lock_irq(&bfqd->lock); + spin_lock_irq(bfqd->lock); bfq_put_async_queues(bfqd, bfqd->root_group); kfree(bfqd->root_group); - spin_unlock_irq(&bfqd->lock); + spin_unlock_irq(bfqd->lock); #endif =20 blk_stat_disable_accounting(bfqd->queue); @@ -7354,7 +7351,7 @@ static int bfq_init_queue(struct request_queue *q, st= ruct elevator_type *e) /* see comments on the definition of next field inside bfq_data */ bfqd->actuator_load_threshold =3D 4; =20 - spin_lock_init(&bfqd->lock); + bfqd->lock =3D &eq->lock; =20 /* * The invocation of the next bfq_create_group_hierarchy diff --git a/block/bfq-iosched.h b/block/bfq-iosched.h index 31217f196f4f..3f612c3e6fae 100644 --- a/block/bfq-iosched.h +++ b/block/bfq-iosched.h @@ -795,7 +795,7 @@ struct bfq_data { /* fallback dummy bfqq for extreme OOM conditions */ struct bfq_queue oom_bfqq; =20 - spinlock_t lock; + spinlock_t *lock; =20 /* * bic associated with the task issuing current bio for diff --git a/block/blk-mq-sched.c b/block/blk-mq-sched.c index 7911fae75ce4..82c4f4eef9ed 100644 --- a/block/blk-mq-sched.c +++ b/block/blk-mq-sched.c @@ -115,10 +115,10 @@ static int __blk_mq_do_dispatch_sched(struct blk_mq_h= w_ctx *hctx) break; =20 if (sq_sched) - spin_lock(&e->lock); + spin_lock_irq(&e->lock); rq =3D e->type->ops.dispatch_request(hctx); if (sq_sched) - spin_unlock(&e->lock); + spin_unlock_irq(&e->lock); =20 if (!rq) { blk_mq_put_dispatch_budget(q, budget_token); --=20 2.39.2 From nobody Sun Oct 5 20:01:09 2025 Received: from dggsgout12.his.huawei.com (dggsgout12.his.huawei.com [45.249.212.56]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AFF092652A9; Wed, 30 Jul 2025 08:29:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.56 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753864146; cv=none; b=Q78oAU993ghX/n4OKbu2SfDNYjCqvuB3gLY+P5oo0a9mitfYihObFGUI8MKCZGVxLStuC4Fjps0jKpM1lPw6kqOo9aG5g2JfBEcFt93++vhaO6j5t20cHSGmyFERyVzE890k3KPw9qsz8G02AAybmLV4iNAsMN6Gf5/+r+StHyA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753864146; c=relaxed/simple; bh=mdNZ5xQybbsbLQx2lXa2qR0eQ4Dm+LCKOn4nHhz7mQI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=mW33wfYvSl/5rRjteZrmMQuOwNoVsgXS0aIX7HG3Tzn6G3XT/AtoUaWM6YmxEm37KPQ1BMx9OvEAP0xuoXAfpwQ+jZDe5weWXVl24rDt4/4m08GxOb9cV+RcIBmLUiFbd9rmlexKLT+R9o+vn5gARRsap+dSjJPjkTqN22tO01A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com; spf=pass smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=45.249.212.56 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.19.163.216]) by dggsgout12.his.huawei.com (SkyGuard) with ESMTPS id 4bsQNZ3tfNzKHMyt; Wed, 30 Jul 2025 16:29:02 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.128]) by mail.maildlp.com (Postfix) with ESMTP id 6E1A31A111D; Wed, 30 Jul 2025 16:29:01 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.104.67]) by APP4 (Coremail) with SMTP id gCh0CgAXkxPI14lokcLlBw--.17899S8; Wed, 30 Jul 2025 16:29:01 +0800 (CST) From: Yu Kuai To: dlemoal@kernel.org, hare@suse.de, jack@suse.cz, tj@kernel.org, josef@toxicpanda.com, axboe@kernel.dk, yukuai3@huawei.com Cc: cgroups@vger.kernel.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, yukuai1@huaweicloud.com, yi.zhang@huawei.com, yangerkun@huawei.com, johnny.chenyi@huawei.com Subject: [PATCH v2 4/5] blk-mq-sched: refactor __blk_mq_do_dispatch_sched() Date: Wed, 30 Jul 2025 16:22:06 +0800 Message-Id: <20250730082207.4031744-5-yukuai1@huaweicloud.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20250730082207.4031744-1-yukuai1@huaweicloud.com> References: <20250730082207.4031744-1-yukuai1@huaweicloud.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 X-CM-TRANSID: gCh0CgAXkxPI14lokcLlBw--.17899S8 X-Coremail-Antispam: 1UD129KBjvJXoWxuw4UWw1DWrWkCry3uryxGrg_yoW3JFWxpF 4fGF43t395XF4jqF929w43Jw1Sy3yxuasrWryrKr4fJwn0qrs8Jrn5JFyUAFs7JrZ5uFZF 9r4DW3s8AFn2vrDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUma14x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_JF0E3s1l82xGYI kIc2x26xkF7I0E14v26ryj6s0DM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2 z4x0Y4vE2Ix0cI8IcVAFwI0_tr0E3s1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr1j6F 4UJwA2z4x0Y4vEx4A2jsIE14v26rxl6s0DM28EF7xvwVC2z280aVCY1x0267AKxVW0oVCq 3wAS0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7 IYx2IY67AKxVWUJVWUGwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6xCaFVCjc4AY6r1j6r4U M4x0Y48IcxkI7VAKI48JM4x0x7Aq67IIx4CEVc8vx2IErcIFxwACI402YVCY1x02628vn2 kIc2xKxwCY1x0262kKe7AKxVWUtVW8ZwCF04k20xvY0x0EwIxGrwCFx2IqxVCFs4IE7xkE bVWUJVW8JwC20s026c02F40E14v26r1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E67 AF67kF1VAFwI0_Jw0_GFylIxkGc2Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVWUJVWUCwCI 42IY6xIIjxv20xvEc7CjxVAFwI0_Cr0_Gr1UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCw CI42IY6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv6xkF7I0E14v26r4UJVWxJrUvcSsG vfC2KfnxnUUI43ZEXa7VUbPC7UUUUUU== X-CM-SenderInfo: 51xn3trlr6x35dzhxuhorxvhhfrp/ Content-Type: text/plain; charset="utf-8" From: Yu Kuai Introduce struct sched_dispatch_ctx, and split the helper into elevator_dispatch_one_request() and elevator_finish_dispatch(). Also and comments about the non-error return value. Make code cleaner, and make it easier to add a new branch to dispatch a batch of requests at a time in the next patch. Signed-off-by: Yu Kuai --- block/blk-mq-sched.c | 196 ++++++++++++++++++++++++++----------------- 1 file changed, 119 insertions(+), 77 deletions(-) diff --git a/block/blk-mq-sched.c b/block/blk-mq-sched.c index 82c4f4eef9ed..f18aecf710ad 100644 --- a/block/blk-mq-sched.c +++ b/block/blk-mq-sched.c @@ -74,91 +74,100 @@ static bool blk_mq_dispatch_hctx_list(struct list_head= *rq_list) =20 #define BLK_MQ_BUDGET_DELAY 3 /* ms units */ =20 -/* - * Only SCSI implements .get_budget and .put_budget, and SCSI restarts - * its queue by itself in its completion handler, so we don't need to - * restart queue if .get_budget() fails to get the budget. - * - * Returns -EAGAIN if hctx->dispatch was found non-empty and run_work has = to - * be run again. This is necessary to avoid starving flushes. - */ -static int __blk_mq_do_dispatch_sched(struct blk_mq_hw_ctx *hctx) -{ - struct request_queue *q =3D hctx->queue; - struct elevator_queue *e =3D q->elevator; - bool multi_hctxs =3D false, run_queue =3D false; - bool dispatched =3D false, busy =3D false; - unsigned int max_dispatch; - LIST_HEAD(rq_list); - int count =3D 0; +struct sched_dispatch_ctx { + struct blk_mq_hw_ctx *hctx; + struct elevator_queue *e; + struct request_queue *q; =20 - if (hctx->dispatch_busy) - max_dispatch =3D 1; - else - max_dispatch =3D hctx->queue->nr_requests; + struct list_head rq_list; + int count; =20 - do { - bool sq_sched =3D blk_queue_sq_sched(q); - struct request *rq; - int budget_token; + bool multi_hctxs; + bool run_queue; + bool busy; +}; =20 - if (e->type->ops.has_work && !e->type->ops.has_work(hctx)) - break; +static bool elevator_can_dispatch(struct sched_dispatch_ctx *ctx) +{ + if (ctx->e->type->ops.has_work && + !ctx->e->type->ops.has_work(ctx->hctx)) + return false; =20 - if (!list_empty_careful(&hctx->dispatch)) { - busy =3D true; - break; - } + if (!list_empty_careful(&ctx->hctx->dispatch)) { + ctx->busy =3D true; + return false; + } =20 - budget_token =3D blk_mq_get_dispatch_budget(q); - if (budget_token < 0) - break; + return true; +} =20 - if (sq_sched) - spin_lock_irq(&e->lock); - rq =3D e->type->ops.dispatch_request(hctx); - if (sq_sched) - spin_unlock_irq(&e->lock); +static bool elevator_dispatch_one_request(struct sched_dispatch_ctx *ctx) +{ + bool sq_sched =3D blk_queue_sq_sched(ctx->q); + struct request *rq; + int budget_token; =20 - if (!rq) { - blk_mq_put_dispatch_budget(q, budget_token); - /* - * We're releasing 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. - */ - run_queue =3D true; - break; - } + if (!elevator_can_dispatch(ctx)) + return false; =20 - blk_mq_set_rq_budget_token(rq, budget_token); + budget_token =3D blk_mq_get_dispatch_budget(ctx->q); + if (budget_token < 0) + return false; =20 - /* - * Now this rq owns the budget which has to be released - * if this rq won't be queued to driver via .queue_rq() - * in blk_mq_dispatch_rq_list(). - */ - list_add_tail(&rq->queuelist, &rq_list); - count++; - if (rq->mq_hctx !=3D hctx) - multi_hctxs =3D true; + if (sq_sched) + spin_lock_irq(&ctx->e->lock); + rq =3D ctx->e->type->ops.dispatch_request(ctx->hctx); + if (sq_sched) + spin_unlock_irq(&ctx->e->lock); =20 + if (!rq) { + blk_mq_put_dispatch_budget(ctx->q, budget_token); /* - * If we cannot get tag for the request, stop dequeueing - * requests from the IO scheduler. We are unlikely to be able - * to submit them anyway and it creates false impression for - * scheduling heuristics that the device can take more IO. + * We're releasing 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. */ - if (!blk_mq_get_driver_tag(rq)) - break; - } while (count < max_dispatch); + ctx->run_queue =3D true; + return false; + } =20 - if (!count) { - if (run_queue) - blk_mq_delay_run_hw_queues(q, BLK_MQ_BUDGET_DELAY); - } else if (multi_hctxs) { + blk_mq_set_rq_budget_token(rq, budget_token); + + /* + * Now this rq owns the budget which has to be released + * if this rq won't be queued to driver via .queue_rq() + * in blk_mq_dispatch_rq_list(). + */ + list_add_tail(&rq->queuelist, &ctx->rq_list); + ctx->count++; + if (rq->mq_hctx !=3D ctx->hctx) + ctx->multi_hctxs =3D true; + + /* + * If we cannot get tag for the request, stop dequeueing + * requests from the IO scheduler. We are unlikely to be able + * to submit them anyway and it creates false impression for + * scheduling heuristics that the device can take more IO. + */ + return blk_mq_get_driver_tag(rq); +} + +/* + * Returns -EAGAIN if hctx->dispatch was found non-empty and run_work has = to + * be run again. This is necessary to avoid starving flushes. + * Return 0 if no request is dispatched. + * Return 1 if at least one request is dispatched. + */ +static int elevator_finish_dispatch(struct sched_dispatch_ctx *ctx) +{ + bool dispatched =3D false; + + if (!ctx->count) { + if (ctx->run_queue) + blk_mq_delay_run_hw_queues(ctx->q, BLK_MQ_BUDGET_DELAY); + } else if (ctx->multi_hctxs) { /* * Requests from different hctx may be dequeued from some * schedulers, such as bfq and deadline. @@ -166,19 +175,52 @@ static int __blk_mq_do_dispatch_sched(struct blk_mq_h= w_ctx *hctx) * Sort the requests in the list according to their hctx, * dispatch batching requests from same hctx at a time. */ - list_sort(NULL, &rq_list, sched_rq_cmp); + list_sort(NULL, &ctx->rq_list, sched_rq_cmp); do { - dispatched |=3D blk_mq_dispatch_hctx_list(&rq_list); - } while (!list_empty(&rq_list)); + dispatched |=3D blk_mq_dispatch_hctx_list(&ctx->rq_list); + } while (!list_empty(&ctx->rq_list)); } else { - dispatched =3D blk_mq_dispatch_rq_list(hctx, &rq_list, false); + dispatched =3D blk_mq_dispatch_rq_list(ctx->hctx, &ctx->rq_list, + false); } =20 - if (busy) + if (ctx->busy) return -EAGAIN; + return !!dispatched; } =20 +/* + * Only SCSI implements .get_budget and .put_budget, and SCSI restarts + * its queue by itself in its completion handler, so we don't need to + * restart queue if .get_budget() fails to get the budget. + * + * See elevator_finish_dispatch() for return values. + */ +static int __blk_mq_do_dispatch_sched(struct blk_mq_hw_ctx *hctx) +{ + unsigned int max_dispatch; + struct sched_dispatch_ctx ctx =3D { + .hctx =3D hctx, + .q =3D hctx->queue, + .e =3D hctx->queue->elevator, + }; + + INIT_LIST_HEAD(&ctx.rq_list); + + if (hctx->dispatch_busy) + max_dispatch =3D 1; + else + max_dispatch =3D hctx->queue->nr_requests; + + do { + if (!elevator_dispatch_one_request(&ctx)) + break; + } while (ctx.count < max_dispatch); + + return elevator_finish_dispatch(&ctx); +} + static int blk_mq_do_dispatch_sched(struct blk_mq_hw_ctx *hctx) { unsigned long end =3D jiffies + HZ; --=20 2.39.2 From nobody Sun Oct 5 20:01:09 2025 Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 34E2E2641F9; Wed, 30 Jul 2025 08:29:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753864147; cv=none; b=GmDjaAO3rdYof7q3y5N2t+I9SQhELdCVcfAkp/OhQyA1OiABuKhdYStp+z9juBfuVv2deoli5jxmLfJ8Ie4MeliwuupDztK/T+vLWJBRGX74TLJ/e0DJHkbj73x5tUOTPXt+36NcxL1MPSBniHexeAURAlXn4viZ9uiYlKRR5t4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753864147; c=relaxed/simple; bh=f2wvEgYM1XikNsWZ7fLrp9n6v3ynezmIBuJfDqurXgg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=f8X4A92fVbVDjU74ONnS22zExyV29fxZQya6RmbnsPy3RG8oRQpjJYqVdaDrnz2Vn3AqgyO1+gmWC2DBnnNfiPoSbCXmf8H2C+jWQ9PGNfBjaEw14kEsYSlgVKLocGgf9PdgtDsFjvpe4n0EFjjF82iZS+vPWazuV+O4qYzWvjY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com; spf=pass smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=45.249.212.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.19.93.142]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTPS id 4bsQNb2ZZ1zYQvLb; Wed, 30 Jul 2025 16:29:03 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.128]) by mail.maildlp.com (Postfix) with ESMTP id 0964E1A14D1; Wed, 30 Jul 2025 16:29:02 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.104.67]) by APP4 (Coremail) with SMTP id gCh0CgAXkxPI14lokcLlBw--.17899S9; Wed, 30 Jul 2025 16:29:01 +0800 (CST) From: Yu Kuai To: dlemoal@kernel.org, hare@suse.de, jack@suse.cz, tj@kernel.org, josef@toxicpanda.com, axboe@kernel.dk, yukuai3@huawei.com Cc: cgroups@vger.kernel.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, yukuai1@huaweicloud.com, yi.zhang@huawei.com, yangerkun@huawei.com, johnny.chenyi@huawei.com Subject: [PATCH v2 5/5] blk-mq-sched: support request batch dispatching for sq elevator Date: Wed, 30 Jul 2025 16:22:07 +0800 Message-Id: <20250730082207.4031744-6-yukuai1@huaweicloud.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20250730082207.4031744-1-yukuai1@huaweicloud.com> References: <20250730082207.4031744-1-yukuai1@huaweicloud.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 X-CM-TRANSID: gCh0CgAXkxPI14lokcLlBw--.17899S9 X-Coremail-Antispam: 1UD129KBjvJXoWxWFWkXFyfCF1kuF15GFW5GFg_yoWrAF1UpF WrJa1FyrW0q3ZFqF9xCw47Jw15Gw4I9r9rWryfKr43JFs7XrsxGr1rJa4UZF4xAr4fCFsr ur4DXas5uF1Iva7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUma14x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_JF0E3s1l82xGYI kIc2x26xkF7I0E14v26ryj6s0DM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2 z4x0Y4vE2Ix0cI8IcVAFwI0_tr0E3s1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr1j6F 4UJwA2z4x0Y4vEx4A2jsIE14v26rxl6s0DM28EF7xvwVC2z280aVCY1x0267AKxVW0oVCq 3wAS0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7 IYx2IY67AKxVWUJVWUGwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6xCaFVCjc4AY6r1j6r4U M4x0Y48IcxkI7VAKI48JM4x0x7Aq67IIx4CEVc8vx2IErcIFxwACI402YVCY1x02628vn2 kIc2xKxwCY1x0262kKe7AKxVWUtVW8ZwCF04k20xvY0x0EwIxGrwCFx2IqxVCFs4IE7xkE bVWUJVW8JwC20s026c02F40E14v26r1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E67 AF67kF1VAFwI0_Jw0_GFylIxkGc2Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVWUCVW8JwCI 42IY6xIIjxv20xvEc7CjxVAFwI0_Cr0_Gr1UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCw CI42IY6I8E87Iv67AKxVW8JVWxJwCI42IY6I8E87Iv6xkF7I0E14v26r4UJVWxJrUvcSsG vfC2KfnxnUUI43ZEXa7VUbPC7UUUUUU== X-CM-SenderInfo: 51xn3trlr6x35dzhxuhorxvhhfrp/ Content-Type: text/plain; charset="utf-8" From: Yu Kuai For dispatch_request method, current behavior is dispatching one request at a time. In the case of multiple dispatching contexts, This behavior, on the one hand, introduce intense lock contention: t1: t2: t3: lock lock lock // grab lock ops.dispatch_request unlock // grab lock ops.dispatch_request unlock // grab lock ops.dispatch_request unlock on the other hand, messing up the requests dispatching order: t1: lock rq1 =3D ops.dispatch_request unlock t2: lock rq2 =3D ops.dispatch_request unlock lock rq3 =3D ops.dispatch_request unlock lock rq4 =3D ops.dispatch_request unlock //rq1,rq3 issue to disk // rq2, rq4 issue to disk In this case, the elevator dispatch order is rq 1-2-3-4, however, such order in disk is rq 1-3-2-4, the order for rq2 and rq3 is inversed. Fix those problems by introducing elevator_dispatch_requests(), this helper will grab the lock and dispatch a batch of requests while holding the lock. Signed-off-by: Yu Kuai --- block/blk-mq-sched.c | 61 +++++++++++++++++++++++++++++++++++++++++--- block/blk-mq.h | 21 +++++++++++++++ 2 files changed, 78 insertions(+), 4 deletions(-) diff --git a/block/blk-mq-sched.c b/block/blk-mq-sched.c index f18aecf710ad..9ee05d6e8350 100644 --- a/block/blk-mq-sched.c +++ b/block/blk-mq-sched.c @@ -101,6 +101,55 @@ static bool elevator_can_dispatch(struct sched_dispatc= h_ctx *ctx) return true; } =20 +static void elevator_dispatch_requests(struct sched_dispatch_ctx *ctx) +{ + bool has_get_budget =3D ctx->q->mq_ops->get_budget !=3D NULL; + int budget_token[BUDGET_TOKEN_BATCH]; + int count =3D ctx->q->nr_requests; + int i; + + while (true) { + if (!elevator_can_dispatch(ctx)) + return; + + if (has_get_budget) { + count =3D blk_mq_get_dispatch_budgets(ctx->q, budget_token); + if (count <=3D 0) + return; + } + + spin_lock_irq(&ctx->e->lock); + for (i =3D 0; i < count; ++i) { + struct request *rq =3D + ctx->e->type->ops.dispatch_request(ctx->hctx); + + if (!rq) { + ctx->run_queue =3D true; + goto err_free_budgets; + } + + if (has_get_budget) + blk_mq_set_rq_budget_token(rq, budget_token[i]); + list_add_tail(&rq->queuelist, &ctx->rq_list); + ctx->count++; + if (rq->mq_hctx !=3D ctx->hctx) + ctx->multi_hctxs =3D true; + + if (!blk_mq_get_driver_tag(rq)) { + i++; + goto err_free_budgets; + } + } + spin_unlock_irq(&ctx->e->lock); + } + +err_free_budgets: + spin_unlock_irq(&ctx->e->lock); + if (has_get_budget) + for (; i < count; ++i) + blk_mq_put_dispatch_budget(ctx->q, budget_token[i]); +} + static bool elevator_dispatch_one_request(struct sched_dispatch_ctx *ctx) { bool sq_sched =3D blk_queue_sq_sched(ctx->q); @@ -213,10 +262,14 @@ static int __blk_mq_do_dispatch_sched(struct blk_mq_h= w_ctx *hctx) else max_dispatch =3D hctx->queue->nr_requests; =20 - do { - if (!elevator_dispatch_one_request(&ctx)) - break; - } while (ctx.count < max_dispatch); + if (!hctx->dispatch_busy && blk_queue_sq_sched(ctx.q)) + elevator_dispatch_requests(&ctx); + else { + do { + if (!elevator_dispatch_one_request(&ctx)) + break; + } while (ctx.count < max_dispatch); + } =20 return elevator_finish_dispatch(&ctx); } diff --git a/block/blk-mq.h b/block/blk-mq.h index affb2e14b56e..450c16a07841 100644 --- a/block/blk-mq.h +++ b/block/blk-mq.h @@ -37,6 +37,7 @@ enum { }; =20 #define BLK_MQ_CPU_WORK_BATCH (8) +#define BUDGET_TOKEN_BATCH (8) =20 typedef unsigned int __bitwise blk_insert_t; #define BLK_MQ_INSERT_AT_HEAD ((__force blk_insert_t)0x01) @@ -262,6 +263,26 @@ static inline int blk_mq_get_dispatch_budget(struct re= quest_queue *q) return 0; } =20 +static inline int blk_mq_get_dispatch_budgets(struct request_queue *q, + int *budget_token) +{ + int count =3D 0; + + while (count < BUDGET_TOKEN_BATCH) { + int token =3D 0; + + if (q->mq_ops->get_budget) + token =3D q->mq_ops->get_budget(q); + + if (token < 0) + return count; + + budget_token[count++] =3D token; + } + + return count; +} + static inline void blk_mq_set_rq_budget_token(struct request *rq, int toke= n) { if (token < 0) --=20 2.39.2