[PATCH RFC v3 02/15] block: add QUEUE_FLAG_BIO_ISSUE

Yu Kuai posted 15 patches 1 month ago
[PATCH RFC v3 02/15] block: add QUEUE_FLAG_BIO_ISSUE
Posted by Yu Kuai 1 month ago
From: Yu Kuai <yukuai3@huawei.com>

blkcg_bio_issue_init() is called for every bio, while initialized
bio_issue_time is only used by io-latency. Add a new queue_flag and
only set this flag when io-latency is initialized, so that extra
blk_time_get_ns() from blkcg_bio_issue_init() can be saved for disks
that io-latency is not enabled.

Signed-off-by: Yu Kuai <yukuai3@huawei.com>
---
 block/blk-cgroup.h     | 5 ++++-
 block/blk-iolatency.c  | 1 +
 block/blk-mq-debugfs.c | 1 +
 include/linux/blkdev.h | 1 +
 4 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/block/blk-cgroup.h b/block/blk-cgroup.h
index d73204d27d72..93e8a9fa76fe 100644
--- a/block/blk-cgroup.h
+++ b/block/blk-cgroup.h
@@ -372,7 +372,10 @@ static inline void blkg_put(struct blkcg_gq *blkg)
 
 static inline void blkcg_bio_issue_init(struct bio *bio)
 {
-	bio->issue_time_ns = blk_time_get_ns();
+	struct request_queue *q = bdev_get_queue(bio->bi_bdev);
+
+	if (test_bit(QUEUE_FLAG_BIO_ISSUE, &q->queue_flags))
+		bio->issue_time_ns = blk_time_get_ns();
 }
 
 static inline void blkcg_use_delay(struct blkcg_gq *blkg)
diff --git a/block/blk-iolatency.c b/block/blk-iolatency.c
index 554b191a6892..c9b3bd12c87c 100644
--- a/block/blk-iolatency.c
+++ b/block/blk-iolatency.c
@@ -767,6 +767,7 @@ static int blk_iolatency_init(struct gendisk *disk)
 	if (ret)
 		goto err_qos_del;
 
+	blk_queue_flag_set(QUEUE_FLAG_BIO_ISSUE, disk->queue);
 	timer_setup(&blkiolat->timer, blkiolatency_timer_fn, 0);
 	INIT_WORK(&blkiolat->enable_work, blkiolatency_enable_work_fn);
 
diff --git a/block/blk-mq-debugfs.c b/block/blk-mq-debugfs.c
index 32c65efdda46..b192647456e1 100644
--- a/block/blk-mq-debugfs.c
+++ b/block/blk-mq-debugfs.c
@@ -96,6 +96,7 @@ static const char *const blk_queue_flag_name[] = {
 	QUEUE_FLAG_NAME(DISABLE_WBT_DEF),
 	QUEUE_FLAG_NAME(NO_ELV_SWITCH),
 	QUEUE_FLAG_NAME(QOS_ENABLED),
+	QUEUE_FLAG_NAME(BIO_ISSUE),
 };
 #undef QUEUE_FLAG_NAME
 
diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
index fe1797bbec42..ca1dcf59cb32 100644
--- a/include/linux/blkdev.h
+++ b/include/linux/blkdev.h
@@ -657,6 +657,7 @@ enum {
 	QUEUE_FLAG_DISABLE_WBT_DEF,	/* for sched to disable/enable wbt */
 	QUEUE_FLAG_NO_ELV_SWITCH,	/* can't switch elevator any more */
 	QUEUE_FLAG_QOS_ENABLED,		/* qos is enabled */
+	QUEUE_FLAG_BIO_ISSUE,		/* track bio issue time */
 	QUEUE_FLAG_MAX
 };
 
-- 
2.39.2
Re: [PATCH RFC v3 02/15] block: add QUEUE_FLAG_BIO_ISSUE
Posted by Christoph Hellwig 4 weeks, 1 day ago
On Mon, Sep 01, 2025 at 11:32:07AM +0800, Yu Kuai wrote:
>  static inline void blkcg_bio_issue_init(struct bio *bio)
>  {
> -	bio->issue_time_ns = blk_time_get_ns();
> +	struct request_queue *q = bdev_get_queue(bio->bi_bdev);
> +
> +	if (test_bit(QUEUE_FLAG_BIO_ISSUE, &q->queue_flags))
> +		bio->issue_time_ns = blk_time_get_ns();
>  }

Given that this is called on a bio and called from generic code
and not blk-mq, the flag should in the gendisk and not the queue.
Re: [PATCH RFC v3 02/15] block: add QUEUE_FLAG_BIO_ISSUE
Posted by Yu Kuai 4 weeks, 1 day ago
Hi,

在 2025/9/3 21:24, Christoph Hellwig 写道:
> On Mon, Sep 01, 2025 at 11:32:07AM +0800, Yu Kuai wrote:
>>   static inline void blkcg_bio_issue_init(struct bio *bio)
>>   {
>> -	bio->issue_time_ns = blk_time_get_ns();
>> +	struct request_queue *q = bdev_get_queue(bio->bi_bdev);
>> +
>> +	if (test_bit(QUEUE_FLAG_BIO_ISSUE, &q->queue_flags))
>> +		bio->issue_time_ns = blk_time_get_ns();
>>   }
> Given that this is called on a bio and called from generic code
> and not blk-mq, the flag should in the gendisk and not the queue.
>
ok, will change to disk, and also change set/clear the flag to
enable/disable iolatency.

Thanks,
Kuai

Re: [PATCH RFC v3 02/15] block: add QUEUE_FLAG_BIO_ISSUE
Posted by Yu Kuai 4 weeks, 1 day ago
Hi,

在 2025/09/04 0:54, Yu Kuai 写道:
> Hi,
> 
> 在 2025/9/3 21:24, Christoph Hellwig 写道:
>> On Mon, Sep 01, 2025 at 11:32:07AM +0800, Yu Kuai wrote:
>>>   static inline void blkcg_bio_issue_init(struct bio *bio)
>>>   {
>>> -    bio->issue_time_ns = blk_time_get_ns();
>>> +    struct request_queue *q = bdev_get_queue(bio->bi_bdev);
>>> +
>>> +    if (test_bit(QUEUE_FLAG_BIO_ISSUE, &q->queue_flags))
>>> +        bio->issue_time_ns = blk_time_get_ns();
>>>   }
>> Given that this is called on a bio and called from generic code
>> and not blk-mq, the flag should in the gendisk and not the queue.
>>
> ok, will change to disk, and also change set/clear the flag to
> enable/disable iolatency.

After careful consideration, I think it's better to delay
blkcg_bio_issue_init() to blk_mq_submit_bio():

- it's only used by iolatency, which can only be enabled for rq-based
disks;
- For bio that is submitted the first time, blk_cgroup_bio_start() is
called from submit_bio_no_acct_nocheck(), where q_usage_counter is not
grabbed yet, hence it's not safe to enable that flag while enabling
iolatency.

diff --git a/block/blk-core.c b/block/blk-core.c
index 4201504158a1..83c262a3dfd9 100644
--- a/block/blk-core.c
+++ b/block/blk-core.c
@@ -728,7 +728,6 @@ static void __submit_bio_noacct_mq(struct bio *bio)
  void submit_bio_noacct_nocheck(struct bio *bio)
  {
         blk_cgroup_bio_start(bio);
-       blkcg_bio_issue_init(bio);

         if (!bio_flagged(bio, BIO_TRACE_COMPLETION)) {
                 trace_block_bio_queue(bio);
diff --git a/block/blk-merge.c b/block/blk-merge.c
index 70d704615be5..5538356770a4 100644
--- a/block/blk-merge.c
+++ b/block/blk-merge.c
@@ -119,7 +119,6 @@ static struct bio *bio_submit_split(struct bio *bio, 
int split_sectors)
                         goto error;
                 }
                 split->bi_opf |= REQ_NOMERGE;
-               blkcg_bio_issue_init(split);
                 bio_chain(split, bio);
                 trace_block_split(split, bio->bi_iter.bi_sector);
                 WARN_ON_ONCE(bio_zone_write_plugging(bio));
diff --git a/block/blk-mq.c b/block/blk-mq.c
index ba3a4b77f578..030937b129a2 100644
--- a/block/blk-mq.c
+++ b/block/blk-mq.c
@@ -3108,7 +3108,7 @@ static bool bio_unaligned(const struct bio *bio, 
struct request_queue *q)
   * It will not queue the request if there is an error with the bio, or 
at the
   * request creation.
   */
-void blk_mq_submit_bio(struct bio *bio)
+void b k_mq_submit_bio(struct bio *bio)
  {
         struct request_queue *q = bdev_get_queue(bio->bi_bdev);
         struct blk_plug *plug = current->plug;
@@ -3168,6 +3168,9 @@ void blk_mq_submit_bio(struct bio *bio)
         if (!bio_integrity_prep(bio))
                 goto queue_exit;

+       if (test_bit(QUEUE_FLAG_BIO_ISSUE, &q->queue_flags))
+               bio->issue_time_ns = blk_time_get_ns();
+
         if (blk_mq_attempt_bio_merge(q, bio, nr_segs))
                 goto queue_exit;


> 
> Thanks,
> Kuai
> 
> .
> 

Re: [PATCH RFC v3 02/15] block: add QUEUE_FLAG_BIO_ISSUE
Posted by Christoph Hellwig 4 weeks, 1 day ago
On Thu, Sep 04, 2025 at 10:44:32AM +0800, Yu Kuai wrote:
> - it's only used by iolatency, which can only be enabled for rq-based
> disks;
> - For bio that is submitted the first time, blk_cgroup_bio_start() is
> called from submit_bio_no_acct_nocheck(), where q_usage_counter is not
> grabbed yet, hence it's not safe to enable that flag while enabling
> iolatency.

Yes, keeping more things in blk-mq is always good.

> -void blk_mq_submit_bio(struct bio *bio)
> +void b k_mq_submit_bio(struct bio *bio)

This got mangled somehow.
Re: [PATCH RFC v3 02/15] block: add QUEUE_FLAG_BIO_ISSUE
Posted by Bart Van Assche 1 month ago
On 8/31/25 8:32 PM, Yu Kuai wrote:
> @@ -372,7 +372,10 @@ static inline void blkg_put(struct blkcg_gq *blkg)
>   
>   static inline void blkcg_bio_issue_init(struct bio *bio)
>   {
> -	bio->issue_time_ns = blk_time_get_ns();
> +	struct request_queue *q = bdev_get_queue(bio->bi_bdev);
> +
> +	if (test_bit(QUEUE_FLAG_BIO_ISSUE, &q->queue_flags))
> +		bio->issue_time_ns = blk_time_get_ns();
>   }
>   
>   static inline void blkcg_use_delay(struct blkcg_gq *blkg)
> diff --git a/block/blk-iolatency.c b/block/blk-iolatency.c
> index 554b191a6892..c9b3bd12c87c 100644
> --- a/block/blk-iolatency.c
> +++ b/block/blk-iolatency.c
> @@ -767,6 +767,7 @@ static int blk_iolatency_init(struct gendisk *disk)
>   	if (ret)
>   		goto err_qos_del;
>   
> +	blk_queue_flag_set(QUEUE_FLAG_BIO_ISSUE, disk->queue);
>   	timer_setup(&blkiolat->timer, blkiolatency_timer_fn, 0);
>   	INIT_WORK(&blkiolat->enable_work, blkiolatency_enable_work_fn);

Shouldn't QUEUE_FLAG_BIO_ISSUE be cleared when initializing
bio->issue_time_ns is no longer necessary?

Thanks,

Bart.
Re: [PATCH RFC v3 02/15] block: add QUEUE_FLAG_BIO_ISSUE
Posted by Yu Kuai 1 month ago
Hi,

在 2025/09/03 1:05, Bart Van Assche 写道:
> On 8/31/25 8:32 PM, Yu Kuai wrote:
>> @@ -372,7 +372,10 @@ static inline void blkg_put(struct blkcg_gq *blkg)
>>   static inline void blkcg_bio_issue_init(struct bio *bio)
>>   {
>> -    bio->issue_time_ns = blk_time_get_ns();
>> +    struct request_queue *q = bdev_get_queue(bio->bi_bdev);
>> +
>> +    if (test_bit(QUEUE_FLAG_BIO_ISSUE, &q->queue_flags))
>> +        bio->issue_time_ns = blk_time_get_ns();
>>   }
>>   static inline void blkcg_use_delay(struct blkcg_gq *blkg)
>> diff --git a/block/blk-iolatency.c b/block/blk-iolatency.c
>> index 554b191a6892..c9b3bd12c87c 100644
>> --- a/block/blk-iolatency.c
>> +++ b/block/blk-iolatency.c
>> @@ -767,6 +767,7 @@ static int blk_iolatency_init(struct gendisk *disk)
>>       if (ret)
>>           goto err_qos_del;
>> +    blk_queue_flag_set(QUEUE_FLAG_BIO_ISSUE, disk->queue);
>>       timer_setup(&blkiolat->timer, blkiolatency_timer_fn, 0);
>>       INIT_WORK(&blkiolat->enable_work, blkiolatency_enable_work_fn);
> 
> Shouldn't QUEUE_FLAG_BIO_ISSUE be cleared when initializing
> bio->issue_time_ns is no longer necessary?
> 

iolatency can never be freed after it's initialized, however, I can add
and clear this flag in blkiolatency_enable_work_fn() instead, when
iolatency is really enabled or discabled.

Thanks,
Kuai

> Thanks,
> 
> Bart.
> 
> .
>