1 // SPDX-License-Identifier: GPL-2.0
3 * blk-mq scheduling framework
5 * Copyright (C) 2016 Jens Axboe
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/blk-mq.h>
10 #include <linux/list_sort.h>
12 #include <trace/events/block.h>
16 #include "blk-mq-debugfs.h"
17 #include "blk-mq-sched.h"
18 #include "blk-mq-tag.h"
22 * Mark a hardware queue as needing a restart.
24 void blk_mq_sched_mark_restart_hctx(struct blk_mq_hw_ctx *hctx)
26 if (test_bit(BLK_MQ_S_SCHED_RESTART, &hctx->state))
29 set_bit(BLK_MQ_S_SCHED_RESTART, &hctx->state);
31 EXPORT_SYMBOL_GPL(blk_mq_sched_mark_restart_hctx);
33 void __blk_mq_sched_restart(struct blk_mq_hw_ctx *hctx)
35 clear_bit(BLK_MQ_S_SCHED_RESTART, &hctx->state);
38 * Order clearing SCHED_RESTART and list_empty_careful(&hctx->dispatch)
39 * in blk_mq_run_hw_queue(). Its pair is the barrier in
40 * blk_mq_dispatch_rq_list(). So dispatch code won't see SCHED_RESTART,
41 * meantime new request added to hctx->dispatch is missed to check in
42 * blk_mq_run_hw_queue().
46 blk_mq_run_hw_queue(hctx, true);
49 static int sched_rq_cmp(void *priv, const struct list_head *a,
50 const struct list_head *b)
52 struct request *rqa = container_of(a, struct request, queuelist);
53 struct request *rqb = container_of(b, struct request, queuelist);
55 return rqa->mq_hctx > rqb->mq_hctx;
58 static bool blk_mq_dispatch_hctx_list(struct list_head *rq_list)
60 struct blk_mq_hw_ctx *hctx =
61 list_first_entry(rq_list, struct request, queuelist)->mq_hctx;
64 unsigned int count = 0;
66 list_for_each_entry(rq, rq_list, queuelist) {
67 if (rq->mq_hctx != hctx) {
68 list_cut_before(&hctx_list, rq_list, &rq->queuelist);
73 list_splice_tail_init(rq_list, &hctx_list);
76 return blk_mq_dispatch_rq_list(hctx, &hctx_list, count);
79 #define BLK_MQ_BUDGET_DELAY 3 /* ms units */
82 * Only SCSI implements .get_budget and .put_budget, and SCSI restarts
83 * its queue by itself in its completion handler, so we don't need to
84 * restart queue if .get_budget() fails to get the budget.
86 * Returns -EAGAIN if hctx->dispatch was found non-empty and run_work has to
87 * be run again. This is necessary to avoid starving flushes.
89 static int __blk_mq_do_dispatch_sched(struct blk_mq_hw_ctx *hctx)
91 struct request_queue *q = hctx->queue;
92 struct elevator_queue *e = q->elevator;
93 bool multi_hctxs = false, run_queue = false;
94 bool dispatched = false, busy = false;
95 unsigned int max_dispatch;
99 if (hctx->dispatch_busy)
102 max_dispatch = hctx->queue->nr_requests;
108 if (e->type->ops.has_work && !e->type->ops.has_work(hctx))
111 if (!list_empty_careful(&hctx->dispatch)) {
116 budget_token = blk_mq_get_dispatch_budget(q);
117 if (budget_token < 0)
120 rq = e->type->ops.dispatch_request(hctx);
122 blk_mq_put_dispatch_budget(q, budget_token);
124 * We're releasing without dispatching. Holding the
125 * budget could have blocked any "hctx"s with the
126 * same queue and if we didn't dispatch then there's
127 * no guarantee anyone will kick the queue. Kick it
134 blk_mq_set_rq_budget_token(rq, budget_token);
137 * Now this rq owns the budget which has to be released
138 * if this rq won't be queued to driver via .queue_rq()
139 * in blk_mq_dispatch_rq_list().
141 list_add_tail(&rq->queuelist, &rq_list);
143 if (rq->mq_hctx != hctx)
147 * If we cannot get tag for the request, stop dequeueing
148 * requests from the IO scheduler. We are unlikely to be able
149 * to submit them anyway and it creates false impression for
150 * scheduling heuristics that the device can take more IO.
152 if (!blk_mq_get_driver_tag(rq))
154 } while (count < max_dispatch);
158 blk_mq_delay_run_hw_queues(q, BLK_MQ_BUDGET_DELAY);
159 } else if (multi_hctxs) {
161 * Requests from different hctx may be dequeued from some
162 * schedulers, such as bfq and deadline.
164 * Sort the requests in the list according to their hctx,
165 * dispatch batching requests from same hctx at a time.
167 list_sort(NULL, &rq_list, sched_rq_cmp);
169 dispatched |= blk_mq_dispatch_hctx_list(&rq_list);
170 } while (!list_empty(&rq_list));
172 dispatched = blk_mq_dispatch_rq_list(hctx, &rq_list, count);
180 static int blk_mq_do_dispatch_sched(struct blk_mq_hw_ctx *hctx)
182 unsigned long end = jiffies + HZ;
186 ret = __blk_mq_do_dispatch_sched(hctx);
189 if (need_resched() || time_is_before_jiffies(end)) {
190 blk_mq_delay_run_hw_queue(hctx, 0);
198 static struct blk_mq_ctx *blk_mq_next_ctx(struct blk_mq_hw_ctx *hctx,
199 struct blk_mq_ctx *ctx)
201 unsigned short idx = ctx->index_hw[hctx->type];
203 if (++idx == hctx->nr_ctx)
206 return hctx->ctxs[idx];
210 * Only SCSI implements .get_budget and .put_budget, and SCSI restarts
211 * its queue by itself in its completion handler, so we don't need to
212 * restart queue if .get_budget() fails to get the budget.
214 * Returns -EAGAIN if hctx->dispatch was found non-empty and run_work has to
215 * be run again. This is necessary to avoid starving flushes.
217 static int blk_mq_do_dispatch_ctx(struct blk_mq_hw_ctx *hctx)
219 struct request_queue *q = hctx->queue;
221 struct blk_mq_ctx *ctx = READ_ONCE(hctx->dispatch_from);
228 if (!list_empty_careful(&hctx->dispatch)) {
233 if (!sbitmap_any_bit_set(&hctx->ctx_map))
236 budget_token = blk_mq_get_dispatch_budget(q);
237 if (budget_token < 0)
240 rq = blk_mq_dequeue_from_ctx(hctx, ctx);
242 blk_mq_put_dispatch_budget(q, budget_token);
244 * We're releasing without dispatching. Holding the
245 * budget could have blocked any "hctx"s with the
246 * same queue and if we didn't dispatch then there's
247 * no guarantee anyone will kick the queue. Kick it
250 blk_mq_delay_run_hw_queues(q, BLK_MQ_BUDGET_DELAY);
254 blk_mq_set_rq_budget_token(rq, budget_token);
257 * Now this rq owns the budget which has to be released
258 * if this rq won't be queued to driver via .queue_rq()
259 * in blk_mq_dispatch_rq_list().
261 list_add(&rq->queuelist, &rq_list);
263 /* round robin for fair dispatch */
264 ctx = blk_mq_next_ctx(hctx, rq->mq_ctx);
266 } while (blk_mq_dispatch_rq_list(rq->mq_hctx, &rq_list, 1));
268 WRITE_ONCE(hctx->dispatch_from, ctx);
272 static int __blk_mq_sched_dispatch_requests(struct blk_mq_hw_ctx *hctx)
274 struct request_queue *q = hctx->queue;
275 const bool has_sched = q->elevator;
280 * If we have previous entries on our dispatch list, grab them first for
281 * more fair dispatch.
283 if (!list_empty_careful(&hctx->dispatch)) {
284 spin_lock(&hctx->lock);
285 if (!list_empty(&hctx->dispatch))
286 list_splice_init(&hctx->dispatch, &rq_list);
287 spin_unlock(&hctx->lock);
291 * Only ask the scheduler for requests, if we didn't have residual
292 * requests from the dispatch list. This is to avoid the case where
293 * we only ever dispatch a fraction of the requests available because
294 * of low device queue depth. Once we pull requests out of the IO
295 * scheduler, we can no longer merge or sort them. So it's best to
296 * leave them there for as long as we can. Mark the hw queue as
297 * needing a restart in that case.
299 * We want to dispatch from the scheduler if there was nothing
300 * on the dispatch list or we were able to dispatch from the
303 if (!list_empty(&rq_list)) {
304 blk_mq_sched_mark_restart_hctx(hctx);
305 if (blk_mq_dispatch_rq_list(hctx, &rq_list, 0)) {
307 ret = blk_mq_do_dispatch_sched(hctx);
309 ret = blk_mq_do_dispatch_ctx(hctx);
311 } else if (has_sched) {
312 ret = blk_mq_do_dispatch_sched(hctx);
313 } else if (hctx->dispatch_busy) {
314 /* dequeue request one by one from sw queue if queue is busy */
315 ret = blk_mq_do_dispatch_ctx(hctx);
317 blk_mq_flush_busy_ctxs(hctx, &rq_list);
318 blk_mq_dispatch_rq_list(hctx, &rq_list, 0);
324 void blk_mq_sched_dispatch_requests(struct blk_mq_hw_ctx *hctx)
326 struct request_queue *q = hctx->queue;
328 /* RCU or SRCU read lock is needed before checking quiesced flag */
329 if (unlikely(blk_mq_hctx_stopped(hctx) || blk_queue_quiesced(q)))
335 * A return of -EAGAIN is an indication that hctx->dispatch is not
336 * empty and we must run again in order to avoid starving flushes.
338 if (__blk_mq_sched_dispatch_requests(hctx) == -EAGAIN) {
339 if (__blk_mq_sched_dispatch_requests(hctx) == -EAGAIN)
340 blk_mq_run_hw_queue(hctx, true);
344 bool blk_mq_sched_bio_merge(struct request_queue *q, struct bio *bio,
345 unsigned int nr_segs)
347 struct elevator_queue *e = q->elevator;
348 struct blk_mq_ctx *ctx;
349 struct blk_mq_hw_ctx *hctx;
353 if (e && e->type->ops.bio_merge) {
354 ret = e->type->ops.bio_merge(q, bio, nr_segs);
358 ctx = blk_mq_get_ctx(q);
359 hctx = blk_mq_map_queue(q, bio->bi_opf, ctx);
361 if (!(hctx->flags & BLK_MQ_F_SHOULD_MERGE) ||
362 list_empty_careful(&ctx->rq_lists[type]))
365 /* default per sw-queue merge */
366 spin_lock(&ctx->lock);
368 * Reverse check our software queue for entries that we could
369 * potentially merge with. Currently includes a hand-wavy stop
370 * count of 8, to not spend too much time checking for merges.
372 if (blk_bio_list_merge(q, &ctx->rq_lists[type], bio, nr_segs))
375 spin_unlock(&ctx->lock);
380 bool blk_mq_sched_try_insert_merge(struct request_queue *q, struct request *rq,
381 struct list_head *free)
383 return rq_mergeable(rq) && elv_attempt_insert_merge(q, rq, free);
385 EXPORT_SYMBOL_GPL(blk_mq_sched_try_insert_merge);
387 static bool blk_mq_sched_bypass_insert(struct blk_mq_hw_ctx *hctx,
391 * dispatch flush and passthrough rq directly
393 * passthrough request has to be added to hctx->dispatch directly.
394 * For some reason, device may be in one situation which can't
395 * handle FS request, so STS_RESOURCE is always returned and the
396 * FS request will be added to hctx->dispatch. However passthrough
397 * request may be required at that time for fixing the problem. If
398 * passthrough request is added to scheduler queue, there isn't any
399 * chance to dispatch it given we prioritize requests in hctx->dispatch.
401 if ((rq->rq_flags & RQF_FLUSH_SEQ) || blk_rq_is_passthrough(rq))
407 void blk_mq_sched_insert_request(struct request *rq, bool at_head,
408 bool run_queue, bool async)
410 struct request_queue *q = rq->q;
411 struct elevator_queue *e = q->elevator;
412 struct blk_mq_ctx *ctx = rq->mq_ctx;
413 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
415 WARN_ON(e && (rq->tag != BLK_MQ_NO_TAG));
417 if (blk_mq_sched_bypass_insert(hctx, rq)) {
419 * Firstly normal IO request is inserted to scheduler queue or
420 * sw queue, meantime we add flush request to dispatch queue(
421 * hctx->dispatch) directly and there is at most one in-flight
422 * flush request for each hw queue, so it doesn't matter to add
423 * flush request to tail or front of the dispatch queue.
425 * Secondly in case of NCQ, flush request belongs to non-NCQ
426 * command, and queueing it will fail when there is any
427 * in-flight normal IO request(NCQ command). When adding flush
428 * rq to the front of hctx->dispatch, it is easier to introduce
429 * extra time to flush rq's latency because of S_SCHED_RESTART
430 * compared with adding to the tail of dispatch queue, then
431 * chance of flush merge is increased, and less flush requests
432 * will be issued to controller. It is observed that ~10% time
433 * is saved in blktests block/004 on disk attached to AHCI/NCQ
434 * drive when adding flush rq to the front of hctx->dispatch.
436 * Simply queue flush rq to the front of hctx->dispatch so that
437 * intensive flush workloads can benefit in case of NCQ HW.
439 at_head = (rq->rq_flags & RQF_FLUSH_SEQ) ? true : at_head;
440 blk_mq_request_bypass_insert(rq, at_head, false);
447 list_add(&rq->queuelist, &list);
448 e->type->ops.insert_requests(hctx, &list, at_head);
450 spin_lock(&ctx->lock);
451 __blk_mq_insert_request(hctx, rq, at_head);
452 spin_unlock(&ctx->lock);
457 blk_mq_run_hw_queue(hctx, async);
460 void blk_mq_sched_insert_requests(struct blk_mq_hw_ctx *hctx,
461 struct blk_mq_ctx *ctx,
462 struct list_head *list, bool run_queue_async)
464 struct elevator_queue *e;
465 struct request_queue *q = hctx->queue;
468 * blk_mq_sched_insert_requests() is called from flush plug
469 * context only, and hold one usage counter to prevent queue
470 * from being released.
472 percpu_ref_get(&q->q_usage_counter);
474 e = hctx->queue->elevator;
476 e->type->ops.insert_requests(hctx, list, false);
479 * try to issue requests directly if the hw queue isn't
480 * busy in case of 'none' scheduler, and this way may save
481 * us one extra enqueue & dequeue to sw queue.
483 if (!hctx->dispatch_busy && !run_queue_async) {
484 blk_mq_run_dispatch_ops(hctx->queue,
485 blk_mq_try_issue_list_directly(hctx, list));
486 if (list_empty(list))
489 blk_mq_insert_requests(hctx, ctx, list);
492 blk_mq_run_hw_queue(hctx, run_queue_async);
494 percpu_ref_put(&q->q_usage_counter);
497 static int blk_mq_sched_alloc_map_and_rqs(struct request_queue *q,
498 struct blk_mq_hw_ctx *hctx,
499 unsigned int hctx_idx)
501 if (blk_mq_is_shared_tags(q->tag_set->flags)) {
502 hctx->sched_tags = q->sched_shared_tags;
506 hctx->sched_tags = blk_mq_alloc_map_and_rqs(q->tag_set, hctx_idx,
509 if (!hctx->sched_tags)
514 static void blk_mq_exit_sched_shared_tags(struct request_queue *queue)
516 blk_mq_free_rq_map(queue->sched_shared_tags);
517 queue->sched_shared_tags = NULL;
520 /* called in queue's release handler, tagset has gone away */
521 static void blk_mq_sched_tags_teardown(struct request_queue *q, unsigned int flags)
523 struct blk_mq_hw_ctx *hctx;
526 queue_for_each_hw_ctx(q, hctx, i) {
527 if (hctx->sched_tags) {
528 if (!blk_mq_is_shared_tags(flags))
529 blk_mq_free_rq_map(hctx->sched_tags);
530 hctx->sched_tags = NULL;
534 if (blk_mq_is_shared_tags(flags))
535 blk_mq_exit_sched_shared_tags(q);
538 static int blk_mq_init_sched_shared_tags(struct request_queue *queue)
540 struct blk_mq_tag_set *set = queue->tag_set;
543 * Set initial depth at max so that we don't need to reallocate for
544 * updating nr_requests.
546 queue->sched_shared_tags = blk_mq_alloc_map_and_rqs(set,
549 if (!queue->sched_shared_tags)
552 blk_mq_tag_update_sched_shared_tags(queue);
557 int blk_mq_init_sched(struct request_queue *q, struct elevator_type *e)
559 unsigned int flags = q->tag_set->flags;
560 struct blk_mq_hw_ctx *hctx;
561 struct elevator_queue *eq;
566 blk_queue_flag_clear(QUEUE_FLAG_SQ_SCHED, q);
568 q->nr_requests = q->tag_set->queue_depth;
573 * Default to double of smaller one between hw queue_depth and 128,
574 * since we don't split into sync/async like the old code did.
575 * Additionally, this is a per-hw queue depth.
577 q->nr_requests = 2 * min_t(unsigned int, q->tag_set->queue_depth,
580 if (blk_mq_is_shared_tags(flags)) {
581 ret = blk_mq_init_sched_shared_tags(q);
586 queue_for_each_hw_ctx(q, hctx, i) {
587 ret = blk_mq_sched_alloc_map_and_rqs(q, hctx, i);
589 goto err_free_map_and_rqs;
592 ret = e->ops.init_sched(q, e);
594 goto err_free_map_and_rqs;
596 mutex_lock(&q->debugfs_mutex);
597 blk_mq_debugfs_register_sched(q);
598 mutex_unlock(&q->debugfs_mutex);
600 queue_for_each_hw_ctx(q, hctx, i) {
601 if (e->ops.init_hctx) {
602 ret = e->ops.init_hctx(hctx, i);
605 blk_mq_sched_free_rqs(q);
606 blk_mq_exit_sched(q, eq);
607 kobject_put(&eq->kobj);
611 mutex_lock(&q->debugfs_mutex);
612 blk_mq_debugfs_register_sched_hctx(q, hctx);
613 mutex_unlock(&q->debugfs_mutex);
618 err_free_map_and_rqs:
619 blk_mq_sched_free_rqs(q);
620 blk_mq_sched_tags_teardown(q, flags);
627 * called in either blk_queue_cleanup or elevator_switch, tagset
628 * is required for freeing requests
630 void blk_mq_sched_free_rqs(struct request_queue *q)
632 struct blk_mq_hw_ctx *hctx;
635 if (blk_mq_is_shared_tags(q->tag_set->flags)) {
636 blk_mq_free_rqs(q->tag_set, q->sched_shared_tags,
639 queue_for_each_hw_ctx(q, hctx, i) {
640 if (hctx->sched_tags)
641 blk_mq_free_rqs(q->tag_set,
642 hctx->sched_tags, i);
647 void blk_mq_exit_sched(struct request_queue *q, struct elevator_queue *e)
649 struct blk_mq_hw_ctx *hctx;
651 unsigned int flags = 0;
653 queue_for_each_hw_ctx(q, hctx, i) {
654 mutex_lock(&q->debugfs_mutex);
655 blk_mq_debugfs_unregister_sched_hctx(hctx);
656 mutex_unlock(&q->debugfs_mutex);
658 if (e->type->ops.exit_hctx && hctx->sched_data) {
659 e->type->ops.exit_hctx(hctx, i);
660 hctx->sched_data = NULL;
665 mutex_lock(&q->debugfs_mutex);
666 blk_mq_debugfs_unregister_sched(q);
667 mutex_unlock(&q->debugfs_mutex);
669 if (e->type->ops.exit_sched)
670 e->type->ops.exit_sched(e);
671 blk_mq_sched_tags_teardown(q, flags);