Skip to content

Commit 5c17f45

Browse files
Chengming Zhouaxboe
authored andcommitted
blk-mq: fix start_time_ns and alloc_time_ns for pre-allocated rq
The iocost rely on rq start_time_ns and alloc_time_ns to tell saturation state of the block device. Most of the time request is allocated after rq_qos_throttle() and its alloc_time_ns or start_time_ns won't be affected. But for plug batched allocation introduced by the commit 47c122e ("block: pre-allocate requests if plug is started and is a batch"), we can rq_qos_throttle() after the allocation of the request. This is what the blk_mq_get_cached_request() does. In this case, the cached request alloc_time_ns or start_time_ns is much ahead if blocked in any qos ->throttle(). Fix it by setting alloc_time_ns and start_time_ns to now when the allocated request is actually used. Signed-off-by: Chengming Zhou <zhouchengming@bytedance.com> Acked-by: Tejun Heo <tj@kernel.org> Reviewed-by: Christoph Hellwig <hch@lst.de> Link: https://lore.kernel.org/r/20230710105516.2053478-1-chengming.zhou@linux.dev Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent f673b4f commit 5c17f45

File tree

1 file changed

+30
-17
lines changed

1 file changed

+30
-17
lines changed

block/blk-mq.c

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,24 @@ void blk_rq_init(struct request_queue *q, struct request *rq)
328328
}
329329
EXPORT_SYMBOL(blk_rq_init);
330330

331+
/* Set start and alloc time when the allocated request is actually used */
332+
static inline void blk_mq_rq_time_init(struct request *rq, u64 alloc_time_ns)
333+
{
334+
if (blk_mq_need_time_stamp(rq))
335+
rq->start_time_ns = ktime_get_ns();
336+
else
337+
rq->start_time_ns = 0;
338+
339+
#ifdef CONFIG_BLK_RQ_ALLOC_TIME
340+
if (blk_queue_rq_alloc_time(rq->q))
341+
rq->alloc_time_ns = alloc_time_ns ?: rq->start_time_ns;
342+
else
343+
rq->alloc_time_ns = 0;
344+
#endif
345+
}
346+
331347
static struct request *blk_mq_rq_ctx_init(struct blk_mq_alloc_data *data,
332-
struct blk_mq_tags *tags, unsigned int tag, u64 alloc_time_ns)
348+
struct blk_mq_tags *tags, unsigned int tag)
333349
{
334350
struct blk_mq_ctx *ctx = data->ctx;
335351
struct blk_mq_hw_ctx *hctx = data->hctx;
@@ -356,14 +372,7 @@ static struct request *blk_mq_rq_ctx_init(struct blk_mq_alloc_data *data,
356372
}
357373
rq->timeout = 0;
358374

359-
if (blk_mq_need_time_stamp(rq))
360-
rq->start_time_ns = ktime_get_ns();
361-
else
362-
rq->start_time_ns = 0;
363375
rq->part = NULL;
364-
#ifdef CONFIG_BLK_RQ_ALLOC_TIME
365-
rq->alloc_time_ns = alloc_time_ns;
366-
#endif
367376
rq->io_start_time_ns = 0;
368377
rq->stats_sectors = 0;
369378
rq->nr_phys_segments = 0;
@@ -393,8 +402,7 @@ static struct request *blk_mq_rq_ctx_init(struct blk_mq_alloc_data *data,
393402
}
394403

395404
static inline struct request *
396-
__blk_mq_alloc_requests_batch(struct blk_mq_alloc_data *data,
397-
u64 alloc_time_ns)
405+
__blk_mq_alloc_requests_batch(struct blk_mq_alloc_data *data)
398406
{
399407
unsigned int tag, tag_offset;
400408
struct blk_mq_tags *tags;
@@ -413,7 +421,7 @@ __blk_mq_alloc_requests_batch(struct blk_mq_alloc_data *data,
413421
tag = tag_offset + i;
414422
prefetch(tags->static_rqs[tag]);
415423
tag_mask &= ~(1UL << i);
416-
rq = blk_mq_rq_ctx_init(data, tags, tag, alloc_time_ns);
424+
rq = blk_mq_rq_ctx_init(data, tags, tag);
417425
rq_list_add(data->cached_rq, rq);
418426
nr++;
419427
}
@@ -474,9 +482,11 @@ static struct request *__blk_mq_alloc_requests(struct blk_mq_alloc_data *data)
474482
* Try batched alloc if we want more than 1 tag.
475483
*/
476484
if (data->nr_tags > 1) {
477-
rq = __blk_mq_alloc_requests_batch(data, alloc_time_ns);
478-
if (rq)
485+
rq = __blk_mq_alloc_requests_batch(data);
486+
if (rq) {
487+
blk_mq_rq_time_init(rq, alloc_time_ns);
479488
return rq;
489+
}
480490
data->nr_tags = 1;
481491
}
482492

@@ -499,8 +509,9 @@ static struct request *__blk_mq_alloc_requests(struct blk_mq_alloc_data *data)
499509
goto retry;
500510
}
501511

502-
return blk_mq_rq_ctx_init(data, blk_mq_tags_from_data(data), tag,
503-
alloc_time_ns);
512+
rq = blk_mq_rq_ctx_init(data, blk_mq_tags_from_data(data), tag);
513+
blk_mq_rq_time_init(rq, alloc_time_ns);
514+
return rq;
504515
}
505516

506517
static struct request *blk_mq_rq_cache_fill(struct request_queue *q,
@@ -555,6 +566,7 @@ static struct request *blk_mq_alloc_cached_request(struct request_queue *q,
555566
return NULL;
556567

557568
plug->cached_rq = rq_list_next(rq);
569+
blk_mq_rq_time_init(rq, 0);
558570
}
559571

560572
rq->cmd_flags = opf;
@@ -656,8 +668,8 @@ struct request *blk_mq_alloc_request_hctx(struct request_queue *q,
656668
tag = blk_mq_get_tag(&data);
657669
if (tag == BLK_MQ_NO_TAG)
658670
goto out_queue_exit;
659-
rq = blk_mq_rq_ctx_init(&data, blk_mq_tags_from_data(&data), tag,
660-
alloc_time_ns);
671+
rq = blk_mq_rq_ctx_init(&data, blk_mq_tags_from_data(&data), tag);
672+
blk_mq_rq_time_init(rq, alloc_time_ns);
661673
rq->__data_len = 0;
662674
rq->__sector = (sector_t) -1;
663675
rq->bio = rq->biotail = NULL;
@@ -2896,6 +2908,7 @@ static inline struct request *blk_mq_get_cached_request(struct request_queue *q,
28962908
plug->cached_rq = rq_list_next(rq);
28972909
rq_qos_throttle(q, *bio);
28982910

2911+
blk_mq_rq_time_init(rq, 0);
28992912
rq->cmd_flags = (*bio)->bi_opf;
29002913
INIT_LIST_HEAD(&rq->queuelist);
29012914
return rq;

0 commit comments

Comments
 (0)