Skip to content

Commit 7d337ee

Browse files
YuKuai-huaweiaxboe
authored andcommitted
blk-mq: fix elevator depth_updated method
Current depth_updated has some problems: 1) depth_updated() will be called for each hctx, while all elevators will update async_depth for the disk level, this is not related to hctx; 2) In blk_mq_update_nr_requests(), if previous hctx update succeed and this hctx update failed, q->nr_requests will not be updated, while async_depth is already updated with new nr_reqeuests in previous depth_updated(); 3) All elevators are using q->nr_requests to calculate async_depth now, however, q->nr_requests is still the old value when depth_updated() is called from blk_mq_update_nr_requests(); Those problems are first from error path, then mq-deadline, and recently for bfq and kyber, fix those problems by: - pass in request_queue instead of hctx; - move depth_updated() after q->nr_requests is updated in blk_mq_update_nr_requests(); - add depth_updated() call inside init_sched() method to initialize async_depth; - remove init_hctx() method for mq-deadline and bfq that is useless now; Fixes: 77f1e0a ("bfq: update internal depth state when queue depth changes") Fixes: 39823b4 ("block/mq-deadline: Fix the tag reservation code") Fixes: 42e6c6c ("lib/sbitmap: convert shallow_depth from one word to the whole sbitmap") Signed-off-by: Yu Kuai <yukuai3@huawei.com> Reviewed-by: Hannes Reinecke <hare@suse.de> Reviewed-by: Li Nan <linan122@huawei.com> Reviewed-by: Nilay Shroff <nilay@linux.ibm.com> Link: https://lore.kernel.org/r/20250821060612.1729939-2-yukuai1@huaweicloud.com Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 225dc96 commit 7d337ee

File tree

6 files changed

+41
-52
lines changed

6 files changed

+41
-52
lines changed

block/bfq-iosched.c

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7109,9 +7109,10 @@ void bfq_put_async_queues(struct bfq_data *bfqd, struct bfq_group *bfqg)
71097109
* See the comments on bfq_limit_depth for the purpose of
71107110
* the depths set in the function. Return minimum shallow depth we'll use.
71117111
*/
7112-
static void bfq_update_depths(struct bfq_data *bfqd, struct sbitmap_queue *bt)
7112+
static void bfq_depth_updated(struct request_queue *q)
71137113
{
7114-
unsigned int nr_requests = bfqd->queue->nr_requests;
7114+
struct bfq_data *bfqd = q->elevator->elevator_data;
7115+
unsigned int nr_requests = q->nr_requests;
71157116

71167117
/*
71177118
* In-word depths if no bfq_queue is being weight-raised:
@@ -7143,21 +7144,8 @@ static void bfq_update_depths(struct bfq_data *bfqd, struct sbitmap_queue *bt)
71437144
bfqd->async_depths[1][0] = max((nr_requests * 3) >> 4, 1U);
71447145
/* no more than ~37% of tags for sync writes (~20% extra tags) */
71457146
bfqd->async_depths[1][1] = max((nr_requests * 6) >> 4, 1U);
7146-
}
7147-
7148-
static void bfq_depth_updated(struct blk_mq_hw_ctx *hctx)
7149-
{
7150-
struct bfq_data *bfqd = hctx->queue->elevator->elevator_data;
7151-
struct blk_mq_tags *tags = hctx->sched_tags;
71527147

7153-
bfq_update_depths(bfqd, &tags->bitmap_tags);
7154-
sbitmap_queue_min_shallow_depth(&tags->bitmap_tags, 1);
7155-
}
7156-
7157-
static int bfq_init_hctx(struct blk_mq_hw_ctx *hctx, unsigned int index)
7158-
{
7159-
bfq_depth_updated(hctx);
7160-
return 0;
7148+
blk_mq_set_min_shallow_depth(q, 1);
71617149
}
71627150

71637151
static void bfq_exit_queue(struct elevator_queue *e)
@@ -7369,6 +7357,7 @@ static int bfq_init_queue(struct request_queue *q, struct elevator_queue *eq)
73697357
goto out_free;
73707358
bfq_init_root_group(bfqd->root_group, bfqd);
73717359
bfq_init_entity(&bfqd->oom_bfqq.entity, bfqd->root_group);
7360+
bfq_depth_updated(q);
73727361

73737362
/* We dispatch from request queue wide instead of hw queue */
73747363
blk_queue_flag_set(QUEUE_FLAG_SQ_SCHED, q);
@@ -7628,7 +7617,6 @@ static struct elevator_type iosched_bfq_mq = {
76287617
.request_merged = bfq_request_merged,
76297618
.has_work = bfq_has_work,
76307619
.depth_updated = bfq_depth_updated,
7631-
.init_hctx = bfq_init_hctx,
76327620
.init_sched = bfq_init_queue,
76337621
.exit_sched = bfq_exit_queue,
76347622
},

block/blk-mq-sched.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,15 @@ static inline bool blk_mq_sched_needs_restart(struct blk_mq_hw_ctx *hctx)
9292
return test_bit(BLK_MQ_S_SCHED_RESTART, &hctx->state);
9393
}
9494

95+
static inline void blk_mq_set_min_shallow_depth(struct request_queue *q,
96+
unsigned int depth)
97+
{
98+
struct blk_mq_hw_ctx *hctx;
99+
unsigned long i;
100+
101+
queue_for_each_hw_ctx(q, hctx, i)
102+
sbitmap_queue_min_shallow_depth(&hctx->sched_tags->bitmap_tags,
103+
depth);
104+
}
105+
95106
#endif

block/blk-mq.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4951,20 +4951,21 @@ int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr)
49514951
false);
49524952
}
49534953
if (ret)
4954-
break;
4955-
if (q->elevator && q->elevator->type->ops.depth_updated)
4956-
q->elevator->type->ops.depth_updated(hctx);
4954+
goto out;
49574955
}
4958-
if (!ret) {
4959-
q->nr_requests = nr;
4960-
if (blk_mq_is_shared_tags(set->flags)) {
4961-
if (q->elevator)
4962-
blk_mq_tag_update_sched_shared_tags(q);
4963-
else
4964-
blk_mq_tag_resize_shared_tags(set, nr);
4965-
}
4956+
4957+
q->nr_requests = nr;
4958+
if (q->elevator && q->elevator->type->ops.depth_updated)
4959+
q->elevator->type->ops.depth_updated(q);
4960+
4961+
if (blk_mq_is_shared_tags(set->flags)) {
4962+
if (q->elevator)
4963+
blk_mq_tag_update_sched_shared_tags(q);
4964+
else
4965+
blk_mq_tag_resize_shared_tags(set, nr);
49664966
}
49674967

4968+
out:
49684969
blk_mq_unquiesce_queue(q);
49694970

49704971
return ret;

block/elevator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ struct elevator_mq_ops {
3737
void (*exit_sched)(struct elevator_queue *);
3838
int (*init_hctx)(struct blk_mq_hw_ctx *, unsigned int);
3939
void (*exit_hctx)(struct blk_mq_hw_ctx *, unsigned int);
40-
void (*depth_updated)(struct blk_mq_hw_ctx *);
40+
void (*depth_updated)(struct request_queue *);
4141

4242
bool (*allow_merge)(struct request_queue *, struct request *, struct bio *);
4343
bool (*bio_merge)(struct request_queue *, struct bio *, unsigned int);

block/kyber-iosched.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,14 @@ static struct kyber_queue_data *kyber_queue_data_alloc(struct request_queue *q)
399399
return ERR_PTR(ret);
400400
}
401401

402+
static void kyber_depth_updated(struct request_queue *q)
403+
{
404+
struct kyber_queue_data *kqd = q->elevator->elevator_data;
405+
406+
kqd->async_depth = q->nr_requests * KYBER_ASYNC_PERCENT / 100U;
407+
blk_mq_set_min_shallow_depth(q, kqd->async_depth);
408+
}
409+
402410
static int kyber_init_sched(struct request_queue *q, struct elevator_queue *eq)
403411
{
404412
struct kyber_queue_data *kqd;
@@ -413,6 +421,7 @@ static int kyber_init_sched(struct request_queue *q, struct elevator_queue *eq)
413421

414422
eq->elevator_data = kqd;
415423
q->elevator = eq;
424+
kyber_depth_updated(q);
416425

417426
return 0;
418427
}
@@ -440,15 +449,6 @@ static void kyber_ctx_queue_init(struct kyber_ctx_queue *kcq)
440449
INIT_LIST_HEAD(&kcq->rq_list[i]);
441450
}
442451

443-
static void kyber_depth_updated(struct blk_mq_hw_ctx *hctx)
444-
{
445-
struct kyber_queue_data *kqd = hctx->queue->elevator->elevator_data;
446-
struct blk_mq_tags *tags = hctx->sched_tags;
447-
448-
kqd->async_depth = hctx->queue->nr_requests * KYBER_ASYNC_PERCENT / 100U;
449-
sbitmap_queue_min_shallow_depth(&tags->bitmap_tags, kqd->async_depth);
450-
}
451-
452452
static int kyber_init_hctx(struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
453453
{
454454
struct kyber_hctx_data *khd;
@@ -493,7 +493,6 @@ static int kyber_init_hctx(struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
493493
khd->batching = 0;
494494

495495
hctx->sched_data = khd;
496-
kyber_depth_updated(hctx);
497496

498497
return 0;
499498

block/mq-deadline.c

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -507,22 +507,12 @@ static void dd_limit_depth(blk_opf_t opf, struct blk_mq_alloc_data *data)
507507
}
508508

509509
/* Called by blk_mq_update_nr_requests(). */
510-
static void dd_depth_updated(struct blk_mq_hw_ctx *hctx)
510+
static void dd_depth_updated(struct request_queue *q)
511511
{
512-
struct request_queue *q = hctx->queue;
513512
struct deadline_data *dd = q->elevator->elevator_data;
514-
struct blk_mq_tags *tags = hctx->sched_tags;
515513

516514
dd->async_depth = q->nr_requests;
517-
518-
sbitmap_queue_min_shallow_depth(&tags->bitmap_tags, 1);
519-
}
520-
521-
/* Called by blk_mq_init_hctx() and blk_mq_init_sched(). */
522-
static int dd_init_hctx(struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
523-
{
524-
dd_depth_updated(hctx);
525-
return 0;
515+
blk_mq_set_min_shallow_depth(q, 1);
526516
}
527517

528518
static void dd_exit_sched(struct elevator_queue *e)
@@ -587,6 +577,7 @@ static int dd_init_sched(struct request_queue *q, struct elevator_queue *eq)
587577
blk_queue_flag_set(QUEUE_FLAG_SQ_SCHED, q);
588578

589579
q->elevator = eq;
580+
dd_depth_updated(q);
590581
return 0;
591582
}
592583

@@ -1048,7 +1039,6 @@ static struct elevator_type mq_deadline = {
10481039
.has_work = dd_has_work,
10491040
.init_sched = dd_init_sched,
10501041
.exit_sched = dd_exit_sched,
1051-
.init_hctx = dd_init_hctx,
10521042
},
10531043

10541044
#ifdef CONFIG_BLK_DEBUG_FS

0 commit comments

Comments
 (0)