13dcf60bcSChristoph Hellwig // SPDX-License-Identifier: GPL-2.0 2bd166ef1SJens Axboe /* 3bd166ef1SJens Axboe * blk-mq scheduling framework 4bd166ef1SJens Axboe * 5bd166ef1SJens Axboe * Copyright (C) 2016 Jens Axboe 6bd166ef1SJens Axboe */ 7bd166ef1SJens Axboe #include <linux/kernel.h> 8bd166ef1SJens Axboe #include <linux/module.h> 9bd166ef1SJens Axboe #include <linux/blk-mq.h> 106e6fcbc2SMing Lei #include <linux/list_sort.h> 11bd166ef1SJens Axboe 12bd166ef1SJens Axboe #include <trace/events/block.h> 13bd166ef1SJens Axboe 14bd166ef1SJens Axboe #include "blk.h" 15bd166ef1SJens Axboe #include "blk-mq.h" 16d332ce09SOmar Sandoval #include "blk-mq-debugfs.h" 17bd166ef1SJens Axboe #include "blk-mq-sched.h" 18bd166ef1SJens Axboe #include "blk-mq-tag.h" 19bd166ef1SJens Axboe #include "blk-wbt.h" 20bd166ef1SJens Axboe 218e8320c9SJens Axboe /* 22*c31e76bcSKemeng Shi * Mark a hardware queue as needing a restart. 238e8320c9SJens Axboe */ 247211aef8SDamien Le Moal void blk_mq_sched_mark_restart_hctx(struct blk_mq_hw_ctx *hctx) 258e8320c9SJens Axboe { 268e8320c9SJens Axboe if (test_bit(BLK_MQ_S_SCHED_RESTART, &hctx->state)) 278e8320c9SJens Axboe return; 288e8320c9SJens Axboe 298e8320c9SJens Axboe set_bit(BLK_MQ_S_SCHED_RESTART, &hctx->state); 308e8320c9SJens Axboe } 317211aef8SDamien Le Moal EXPORT_SYMBOL_GPL(blk_mq_sched_mark_restart_hctx); 328e8320c9SJens Axboe 33e9ea1596SPavel Begunkov void __blk_mq_sched_restart(struct blk_mq_hw_ctx *hctx) 348e8320c9SJens Axboe { 358e8320c9SJens Axboe clear_bit(BLK_MQ_S_SCHED_RESTART, &hctx->state); 368e8320c9SJens Axboe 37d7d8535fSMing Lei /* 38d7d8535fSMing Lei * Order clearing SCHED_RESTART and list_empty_careful(&hctx->dispatch) 39d7d8535fSMing Lei * in blk_mq_run_hw_queue(). Its pair is the barrier in 40d7d8535fSMing Lei * blk_mq_dispatch_rq_list(). So dispatch code won't see SCHED_RESTART, 41d7d8535fSMing Lei * meantime new request added to hctx->dispatch is missed to check in 42d7d8535fSMing Lei * blk_mq_run_hw_queue(). 43d7d8535fSMing Lei */ 44d7d8535fSMing Lei smp_mb(); 45d7d8535fSMing Lei 4697889f9aSMing Lei blk_mq_run_hw_queue(hctx, true); 478e8320c9SJens Axboe } 488e8320c9SJens Axboe 494f0f586bSSami Tolvanen static int sched_rq_cmp(void *priv, const struct list_head *a, 504f0f586bSSami Tolvanen const struct list_head *b) 516e6fcbc2SMing Lei { 526e6fcbc2SMing Lei struct request *rqa = container_of(a, struct request, queuelist); 536e6fcbc2SMing Lei struct request *rqb = container_of(b, struct request, queuelist); 546e6fcbc2SMing Lei 556e6fcbc2SMing Lei return rqa->mq_hctx > rqb->mq_hctx; 566e6fcbc2SMing Lei } 576e6fcbc2SMing Lei 586e6fcbc2SMing Lei static bool blk_mq_dispatch_hctx_list(struct list_head *rq_list) 596e6fcbc2SMing Lei { 606e6fcbc2SMing Lei struct blk_mq_hw_ctx *hctx = 616e6fcbc2SMing Lei list_first_entry(rq_list, struct request, queuelist)->mq_hctx; 626e6fcbc2SMing Lei struct request *rq; 636e6fcbc2SMing Lei LIST_HEAD(hctx_list); 646e6fcbc2SMing Lei unsigned int count = 0; 656e6fcbc2SMing Lei 666e6fcbc2SMing Lei list_for_each_entry(rq, rq_list, queuelist) { 676e6fcbc2SMing Lei if (rq->mq_hctx != hctx) { 686e6fcbc2SMing Lei list_cut_before(&hctx_list, rq_list, &rq->queuelist); 696e6fcbc2SMing Lei goto dispatch; 706e6fcbc2SMing Lei } 716e6fcbc2SMing Lei count++; 726e6fcbc2SMing Lei } 736e6fcbc2SMing Lei list_splice_tail_init(rq_list, &hctx_list); 746e6fcbc2SMing Lei 756e6fcbc2SMing Lei dispatch: 76106e71c5SBaolin Wang return blk_mq_dispatch_rq_list(hctx, &hctx_list, count); 776e6fcbc2SMing Lei } 786e6fcbc2SMing Lei 79a0823421SDouglas Anderson #define BLK_MQ_BUDGET_DELAY 3 /* ms units */ 80a0823421SDouglas Anderson 811f460b63SMing Lei /* 821f460b63SMing Lei * Only SCSI implements .get_budget and .put_budget, and SCSI restarts 831f460b63SMing Lei * its queue by itself in its completion handler, so we don't need to 841f460b63SMing Lei * restart queue if .get_budget() returns BLK_STS_NO_RESOURCE. 8528d65729SSalman Qazi * 8628d65729SSalman Qazi * Returns -EAGAIN if hctx->dispatch was found non-empty and run_work has to 8728d65729SSalman Qazi * be run again. This is necessary to avoid starving flushes. 881f460b63SMing Lei */ 896e6fcbc2SMing Lei static int __blk_mq_do_dispatch_sched(struct blk_mq_hw_ctx *hctx) 90caf8eb0dSMing Lei { 91caf8eb0dSMing Lei struct request_queue *q = hctx->queue; 92caf8eb0dSMing Lei struct elevator_queue *e = q->elevator; 936e6fcbc2SMing Lei bool multi_hctxs = false, run_queue = false; 946e6fcbc2SMing Lei bool dispatched = false, busy = false; 956e6fcbc2SMing Lei unsigned int max_dispatch; 96caf8eb0dSMing Lei LIST_HEAD(rq_list); 976e6fcbc2SMing Lei int count = 0; 986e6fcbc2SMing Lei 996e6fcbc2SMing Lei if (hctx->dispatch_busy) 1006e6fcbc2SMing Lei max_dispatch = 1; 1016e6fcbc2SMing Lei else 1026e6fcbc2SMing Lei max_dispatch = hctx->queue->nr_requests; 103caf8eb0dSMing Lei 104445874e8SMing Lei do { 1056e6fcbc2SMing Lei struct request *rq; 1062a5a24aaSMing Lei int budget_token; 1076e6fcbc2SMing Lei 108f9cd4bfeSJens Axboe if (e->type->ops.has_work && !e->type->ops.has_work(hctx)) 109caf8eb0dSMing Lei break; 110de148297SMing Lei 11128d65729SSalman Qazi if (!list_empty_careful(&hctx->dispatch)) { 1126e6fcbc2SMing Lei busy = true; 11328d65729SSalman Qazi break; 11428d65729SSalman Qazi } 11528d65729SSalman Qazi 1162a5a24aaSMing Lei budget_token = blk_mq_get_dispatch_budget(q); 1172a5a24aaSMing Lei if (budget_token < 0) 1181f460b63SMing Lei break; 119de148297SMing Lei 120f9cd4bfeSJens Axboe rq = e->type->ops.dispatch_request(hctx); 121de148297SMing Lei if (!rq) { 1222a5a24aaSMing Lei blk_mq_put_dispatch_budget(q, budget_token); 123a0823421SDouglas Anderson /* 124a0823421SDouglas Anderson * We're releasing without dispatching. Holding the 125a0823421SDouglas Anderson * budget could have blocked any "hctx"s with the 126a0823421SDouglas Anderson * same queue and if we didn't dispatch then there's 127a0823421SDouglas Anderson * no guarantee anyone will kick the queue. Kick it 128a0823421SDouglas Anderson * ourselves. 129a0823421SDouglas Anderson */ 1306e6fcbc2SMing Lei run_queue = true; 131de148297SMing Lei break; 132caf8eb0dSMing Lei } 133caf8eb0dSMing Lei 1342a5a24aaSMing Lei blk_mq_set_rq_budget_token(rq, budget_token); 1352a5a24aaSMing Lei 136de148297SMing Lei /* 137de148297SMing Lei * Now this rq owns the budget which has to be released 138de148297SMing Lei * if this rq won't be queued to driver via .queue_rq() 139de148297SMing Lei * in blk_mq_dispatch_rq_list(). 140de148297SMing Lei */ 1416e6fcbc2SMing Lei list_add_tail(&rq->queuelist, &rq_list); 14261347154SJan Kara count++; 1436e6fcbc2SMing Lei if (rq->mq_hctx != hctx) 1446e6fcbc2SMing Lei multi_hctxs = true; 14561347154SJan Kara 14661347154SJan Kara /* 14761347154SJan Kara * If we cannot get tag for the request, stop dequeueing 14861347154SJan Kara * requests from the IO scheduler. We are unlikely to be able 14961347154SJan Kara * to submit them anyway and it creates false impression for 15061347154SJan Kara * scheduling heuristics that the device can take more IO. 15161347154SJan Kara */ 15261347154SJan Kara if (!blk_mq_get_driver_tag(rq)) 15361347154SJan Kara break; 15461347154SJan Kara } while (count < max_dispatch); 1556e6fcbc2SMing Lei 1566e6fcbc2SMing Lei if (!count) { 1576e6fcbc2SMing Lei if (run_queue) 1586e6fcbc2SMing Lei blk_mq_delay_run_hw_queues(q, BLK_MQ_BUDGET_DELAY); 1596e6fcbc2SMing Lei } else if (multi_hctxs) { 1606e6fcbc2SMing Lei /* 1616e6fcbc2SMing Lei * Requests from different hctx may be dequeued from some 1626e6fcbc2SMing Lei * schedulers, such as bfq and deadline. 1636e6fcbc2SMing Lei * 1646e6fcbc2SMing Lei * Sort the requests in the list according to their hctx, 1656e6fcbc2SMing Lei * dispatch batching requests from same hctx at a time. 1666e6fcbc2SMing Lei */ 1676e6fcbc2SMing Lei list_sort(NULL, &rq_list, sched_rq_cmp); 1686e6fcbc2SMing Lei do { 1696e6fcbc2SMing Lei dispatched |= blk_mq_dispatch_hctx_list(&rq_list); 1706e6fcbc2SMing Lei } while (!list_empty(&rq_list)); 1716e6fcbc2SMing Lei } else { 1726e6fcbc2SMing Lei dispatched = blk_mq_dispatch_rq_list(hctx, &rq_list, count); 1736e6fcbc2SMing Lei } 1746e6fcbc2SMing Lei 1756e6fcbc2SMing Lei if (busy) 1766e6fcbc2SMing Lei return -EAGAIN; 1776e6fcbc2SMing Lei return !!dispatched; 1786e6fcbc2SMing Lei } 1796e6fcbc2SMing Lei 1806e6fcbc2SMing Lei static int blk_mq_do_dispatch_sched(struct blk_mq_hw_ctx *hctx) 1816e6fcbc2SMing Lei { 182572299f0SShin'ichiro Kawasaki unsigned long end = jiffies + HZ; 1836e6fcbc2SMing Lei int ret; 1846e6fcbc2SMing Lei 1856e6fcbc2SMing Lei do { 1866e6fcbc2SMing Lei ret = __blk_mq_do_dispatch_sched(hctx); 187572299f0SShin'ichiro Kawasaki if (ret != 1) 188572299f0SShin'ichiro Kawasaki break; 189572299f0SShin'ichiro Kawasaki if (need_resched() || time_is_before_jiffies(end)) { 190572299f0SShin'ichiro Kawasaki blk_mq_delay_run_hw_queue(hctx, 0); 191572299f0SShin'ichiro Kawasaki break; 192572299f0SShin'ichiro Kawasaki } 193572299f0SShin'ichiro Kawasaki } while (1); 19428d65729SSalman Qazi 19528d65729SSalman Qazi return ret; 196de148297SMing Lei } 197de148297SMing Lei 198b347689fSMing Lei static struct blk_mq_ctx *blk_mq_next_ctx(struct blk_mq_hw_ctx *hctx, 199b347689fSMing Lei struct blk_mq_ctx *ctx) 200b347689fSMing Lei { 201f31967f0SJens Axboe unsigned short idx = ctx->index_hw[hctx->type]; 202b347689fSMing Lei 203b347689fSMing Lei if (++idx == hctx->nr_ctx) 204b347689fSMing Lei idx = 0; 205b347689fSMing Lei 206b347689fSMing Lei return hctx->ctxs[idx]; 207b347689fSMing Lei } 208b347689fSMing Lei 2091f460b63SMing Lei /* 2101f460b63SMing Lei * Only SCSI implements .get_budget and .put_budget, and SCSI restarts 2111f460b63SMing Lei * its queue by itself in its completion handler, so we don't need to 2121f460b63SMing Lei * restart queue if .get_budget() returns BLK_STS_NO_RESOURCE. 21328d65729SSalman Qazi * 21428d65729SSalman Qazi * Returns -EAGAIN if hctx->dispatch was found non-empty and run_work has to 215c4aecaa2SRandy Dunlap * be run again. This is necessary to avoid starving flushes. 2161f460b63SMing Lei */ 21728d65729SSalman Qazi static int blk_mq_do_dispatch_ctx(struct blk_mq_hw_ctx *hctx) 218b347689fSMing Lei { 219b347689fSMing Lei struct request_queue *q = hctx->queue; 220b347689fSMing Lei LIST_HEAD(rq_list); 221b347689fSMing Lei struct blk_mq_ctx *ctx = READ_ONCE(hctx->dispatch_from); 22228d65729SSalman Qazi int ret = 0; 223b347689fSMing Lei struct request *rq; 224b347689fSMing Lei 225445874e8SMing Lei do { 2262a5a24aaSMing Lei int budget_token; 2272a5a24aaSMing Lei 22828d65729SSalman Qazi if (!list_empty_careful(&hctx->dispatch)) { 22928d65729SSalman Qazi ret = -EAGAIN; 23028d65729SSalman Qazi break; 23128d65729SSalman Qazi } 23228d65729SSalman Qazi 233b347689fSMing Lei if (!sbitmap_any_bit_set(&hctx->ctx_map)) 234b347689fSMing Lei break; 235b347689fSMing Lei 2362a5a24aaSMing Lei budget_token = blk_mq_get_dispatch_budget(q); 2372a5a24aaSMing Lei if (budget_token < 0) 2381f460b63SMing Lei break; 239b347689fSMing Lei 240b347689fSMing Lei rq = blk_mq_dequeue_from_ctx(hctx, ctx); 241b347689fSMing Lei if (!rq) { 2422a5a24aaSMing Lei blk_mq_put_dispatch_budget(q, budget_token); 243a0823421SDouglas Anderson /* 244a0823421SDouglas Anderson * We're releasing without dispatching. Holding the 245a0823421SDouglas Anderson * budget could have blocked any "hctx"s with the 246a0823421SDouglas Anderson * same queue and if we didn't dispatch then there's 247a0823421SDouglas Anderson * no guarantee anyone will kick the queue. Kick it 248a0823421SDouglas Anderson * ourselves. 249a0823421SDouglas Anderson */ 250a0823421SDouglas Anderson blk_mq_delay_run_hw_queues(q, BLK_MQ_BUDGET_DELAY); 251b347689fSMing Lei break; 252b347689fSMing Lei } 253b347689fSMing Lei 2542a5a24aaSMing Lei blk_mq_set_rq_budget_token(rq, budget_token); 2552a5a24aaSMing Lei 256b347689fSMing Lei /* 257b347689fSMing Lei * Now this rq owns the budget which has to be released 258b347689fSMing Lei * if this rq won't be queued to driver via .queue_rq() 259b347689fSMing Lei * in blk_mq_dispatch_rq_list(). 260b347689fSMing Lei */ 261b347689fSMing Lei list_add(&rq->queuelist, &rq_list); 262b347689fSMing Lei 263b347689fSMing Lei /* round robin for fair dispatch */ 264b347689fSMing Lei ctx = blk_mq_next_ctx(hctx, rq->mq_ctx); 265b347689fSMing Lei 2661fd40b5eSMing Lei } while (blk_mq_dispatch_rq_list(rq->mq_hctx, &rq_list, 1)); 267b347689fSMing Lei 268b347689fSMing Lei WRITE_ONCE(hctx->dispatch_from, ctx); 26928d65729SSalman Qazi return ret; 270b347689fSMing Lei } 271b347689fSMing Lei 272e1b586f2SZheng Bin static int __blk_mq_sched_dispatch_requests(struct blk_mq_hw_ctx *hctx) 273bd166ef1SJens Axboe { 27481380ca1SOmar Sandoval struct request_queue *q = hctx->queue; 275e42cfb1dSDamien Le Moal const bool has_sched = q->elevator; 27628d65729SSalman Qazi int ret = 0; 277bd166ef1SJens Axboe LIST_HEAD(rq_list); 278bd166ef1SJens Axboe 279bd166ef1SJens Axboe /* 280bd166ef1SJens Axboe * If we have previous entries on our dispatch list, grab them first for 281bd166ef1SJens Axboe * more fair dispatch. 282bd166ef1SJens Axboe */ 283bd166ef1SJens Axboe if (!list_empty_careful(&hctx->dispatch)) { 284bd166ef1SJens Axboe spin_lock(&hctx->lock); 285bd166ef1SJens Axboe if (!list_empty(&hctx->dispatch)) 286bd166ef1SJens Axboe list_splice_init(&hctx->dispatch, &rq_list); 287bd166ef1SJens Axboe spin_unlock(&hctx->lock); 288bd166ef1SJens Axboe } 289bd166ef1SJens Axboe 290bd166ef1SJens Axboe /* 291bd166ef1SJens Axboe * Only ask the scheduler for requests, if we didn't have residual 292bd166ef1SJens Axboe * requests from the dispatch list. This is to avoid the case where 293bd166ef1SJens Axboe * we only ever dispatch a fraction of the requests available because 294bd166ef1SJens Axboe * of low device queue depth. Once we pull requests out of the IO 295bd166ef1SJens Axboe * scheduler, we can no longer merge or sort them. So it's best to 296bd166ef1SJens Axboe * leave them there for as long as we can. Mark the hw queue as 297bd166ef1SJens Axboe * needing a restart in that case. 298caf8eb0dSMing Lei * 2995e3d02bbSMing Lei * We want to dispatch from the scheduler if there was nothing 3005e3d02bbSMing Lei * on the dispatch list or we were able to dispatch from the 3015e3d02bbSMing Lei * dispatch list. 30264765a75SJens Axboe */ 303caf8eb0dSMing Lei if (!list_empty(&rq_list)) { 304caf8eb0dSMing Lei blk_mq_sched_mark_restart_hctx(hctx); 3051fd40b5eSMing Lei if (blk_mq_dispatch_rq_list(hctx, &rq_list, 0)) { 306e42cfb1dSDamien Le Moal if (has_sched) 30728d65729SSalman Qazi ret = blk_mq_do_dispatch_sched(hctx); 308b347689fSMing Lei else 30928d65729SSalman Qazi ret = blk_mq_do_dispatch_ctx(hctx); 310b347689fSMing Lei } 311e42cfb1dSDamien Le Moal } else if (has_sched) { 31228d65729SSalman Qazi ret = blk_mq_do_dispatch_sched(hctx); 3136e768717SMing Lei } else if (hctx->dispatch_busy) { 3146e768717SMing Lei /* dequeue request one by one from sw queue if queue is busy */ 31528d65729SSalman Qazi ret = blk_mq_do_dispatch_ctx(hctx); 316caf8eb0dSMing Lei } else { 317caf8eb0dSMing Lei blk_mq_flush_busy_ctxs(hctx, &rq_list); 3181fd40b5eSMing Lei blk_mq_dispatch_rq_list(hctx, &rq_list, 0); 319c13660a0SJens Axboe } 32028d65729SSalman Qazi 32128d65729SSalman Qazi return ret; 32228d65729SSalman Qazi } 32328d65729SSalman Qazi 32428d65729SSalman Qazi void blk_mq_sched_dispatch_requests(struct blk_mq_hw_ctx *hctx) 32528d65729SSalman Qazi { 32628d65729SSalman Qazi struct request_queue *q = hctx->queue; 32728d65729SSalman Qazi 32828d65729SSalman Qazi /* RCU or SRCU read lock is needed before checking quiesced flag */ 32928d65729SSalman Qazi if (unlikely(blk_mq_hctx_stopped(hctx) || blk_queue_quiesced(q))) 33028d65729SSalman Qazi return; 33128d65729SSalman Qazi 33228d65729SSalman Qazi hctx->run++; 33328d65729SSalman Qazi 33428d65729SSalman Qazi /* 33528d65729SSalman Qazi * A return of -EAGAIN is an indication that hctx->dispatch is not 33628d65729SSalman Qazi * empty and we must run again in order to avoid starving flushes. 33728d65729SSalman Qazi */ 33828d65729SSalman Qazi if (__blk_mq_sched_dispatch_requests(hctx) == -EAGAIN) { 33928d65729SSalman Qazi if (__blk_mq_sched_dispatch_requests(hctx) == -EAGAIN) 34028d65729SSalman Qazi blk_mq_run_hw_queue(hctx, true); 34128d65729SSalman Qazi } 342bd166ef1SJens Axboe } 343bd166ef1SJens Axboe 344179ae84fSPavel Begunkov bool blk_mq_sched_bio_merge(struct request_queue *q, struct bio *bio, 34514ccb66bSChristoph Hellwig unsigned int nr_segs) 346bd166ef1SJens Axboe { 347bd166ef1SJens Axboe struct elevator_queue *e = q->elevator; 348efed9a33SOmar Sandoval struct blk_mq_ctx *ctx; 349efed9a33SOmar Sandoval struct blk_mq_hw_ctx *hctx; 3509bddeb2aSMing Lei bool ret = false; 351c16d6b5aSMing Lei enum hctx_type type; 352bd166ef1SJens Axboe 353900e0807SJens Axboe if (e && e->type->ops.bio_merge) { 354900e0807SJens Axboe ret = e->type->ops.bio_merge(q, bio, nr_segs); 355900e0807SJens Axboe goto out_put; 356900e0807SJens Axboe } 357bd166ef1SJens Axboe 358efed9a33SOmar Sandoval ctx = blk_mq_get_ctx(q); 359efed9a33SOmar Sandoval hctx = blk_mq_map_queue(q, bio->bi_opf, ctx); 360c16d6b5aSMing Lei type = hctx->type; 361cdfcef9eSBaolin Wang if (!(hctx->flags & BLK_MQ_F_SHOULD_MERGE) || 362cdfcef9eSBaolin Wang list_empty_careful(&ctx->rq_lists[type])) 363900e0807SJens Axboe goto out_put; 364cdfcef9eSBaolin Wang 3659bddeb2aSMing Lei /* default per sw-queue merge */ 3669bddeb2aSMing Lei spin_lock(&ctx->lock); 367cdfcef9eSBaolin Wang /* 368cdfcef9eSBaolin Wang * Reverse check our software queue for entries that we could 369cdfcef9eSBaolin Wang * potentially merge with. Currently includes a hand-wavy stop 370cdfcef9eSBaolin Wang * count of 8, to not spend too much time checking for merges. 371cdfcef9eSBaolin Wang */ 3729a14d6ceSJens Axboe if (blk_bio_list_merge(q, &ctx->rq_lists[type], bio, nr_segs)) 373cdfcef9eSBaolin Wang ret = true; 3749bddeb2aSMing Lei 375cdfcef9eSBaolin Wang spin_unlock(&ctx->lock); 376900e0807SJens Axboe out_put: 3779bddeb2aSMing Lei return ret; 378bd166ef1SJens Axboe } 379bd166ef1SJens Axboe 380fd2ef39cSJan Kara bool blk_mq_sched_try_insert_merge(struct request_queue *q, struct request *rq, 381fd2ef39cSJan Kara struct list_head *free) 382bd166ef1SJens Axboe { 383fd2ef39cSJan Kara return rq_mergeable(rq) && elv_attempt_insert_merge(q, rq, free); 384bd166ef1SJens Axboe } 385bd166ef1SJens Axboe EXPORT_SYMBOL_GPL(blk_mq_sched_try_insert_merge); 386bd166ef1SJens Axboe 3870cacba6cSOmar Sandoval static bool blk_mq_sched_bypass_insert(struct blk_mq_hw_ctx *hctx, 3880cacba6cSOmar Sandoval struct request *rq) 389bd166ef1SJens Axboe { 39001e99aecSMing Lei /* 39101e99aecSMing Lei * dispatch flush and passthrough rq directly 39201e99aecSMing Lei * 39301e99aecSMing Lei * passthrough request has to be added to hctx->dispatch directly. 39401e99aecSMing Lei * For some reason, device may be in one situation which can't 39501e99aecSMing Lei * handle FS request, so STS_RESOURCE is always returned and the 39601e99aecSMing Lei * FS request will be added to hctx->dispatch. However passthrough 39701e99aecSMing Lei * request may be required at that time for fixing the problem. If 39801e99aecSMing Lei * passthrough request is added to scheduler queue, there isn't any 39901e99aecSMing Lei * chance to dispatch it given we prioritize requests in hctx->dispatch. 40001e99aecSMing Lei */ 40101e99aecSMing Lei if ((rq->rq_flags & RQF_FLUSH_SEQ) || blk_rq_is_passthrough(rq)) 402bd166ef1SJens Axboe return true; 403bd166ef1SJens Axboe 404a6a252e6SMing Lei return false; 405a6a252e6SMing Lei } 406a6a252e6SMing Lei 407bd6737f1SJens Axboe void blk_mq_sched_insert_request(struct request *rq, bool at_head, 4089e97d295SMike Snitzer bool run_queue, bool async) 409bd6737f1SJens Axboe { 410bd6737f1SJens Axboe struct request_queue *q = rq->q; 411bd6737f1SJens Axboe struct elevator_queue *e = q->elevator; 412bd6737f1SJens Axboe struct blk_mq_ctx *ctx = rq->mq_ctx; 413ea4f995eSJens Axboe struct blk_mq_hw_ctx *hctx = rq->mq_hctx; 414bd6737f1SJens Axboe 415e44a6a23SXianting Tian WARN_ON(e && (rq->tag != BLK_MQ_NO_TAG)); 416923218f6SMing Lei 4175218e12eSJean Delvare if (blk_mq_sched_bypass_insert(hctx, rq)) { 418cc3200eaSMing Lei /* 419cc3200eaSMing Lei * Firstly normal IO request is inserted to scheduler queue or 420cc3200eaSMing Lei * sw queue, meantime we add flush request to dispatch queue( 421cc3200eaSMing Lei * hctx->dispatch) directly and there is at most one in-flight 422cc3200eaSMing Lei * flush request for each hw queue, so it doesn't matter to add 423cc3200eaSMing Lei * flush request to tail or front of the dispatch queue. 424cc3200eaSMing Lei * 425cc3200eaSMing Lei * Secondly in case of NCQ, flush request belongs to non-NCQ 426cc3200eaSMing Lei * command, and queueing it will fail when there is any 427cc3200eaSMing Lei * in-flight normal IO request(NCQ command). When adding flush 428cc3200eaSMing Lei * rq to the front of hctx->dispatch, it is easier to introduce 429cc3200eaSMing Lei * extra time to flush rq's latency because of S_SCHED_RESTART 430cc3200eaSMing Lei * compared with adding to the tail of dispatch queue, then 431cc3200eaSMing Lei * chance of flush merge is increased, and less flush requests 432cc3200eaSMing Lei * will be issued to controller. It is observed that ~10% time 433cc3200eaSMing Lei * is saved in blktests block/004 on disk attached to AHCI/NCQ 434cc3200eaSMing Lei * drive when adding flush rq to the front of hctx->dispatch. 435cc3200eaSMing Lei * 436cc3200eaSMing Lei * Simply queue flush rq to the front of hctx->dispatch so that 437cc3200eaSMing Lei * intensive flush workloads can benefit in case of NCQ HW. 438cc3200eaSMing Lei */ 439cc3200eaSMing Lei at_head = (rq->rq_flags & RQF_FLUSH_SEQ) ? true : at_head; 44001e99aecSMing Lei blk_mq_request_bypass_insert(rq, at_head, false); 4410cacba6cSOmar Sandoval goto run; 44201e99aecSMing Lei } 4430cacba6cSOmar Sandoval 444e42cfb1dSDamien Le Moal if (e) { 445bd6737f1SJens Axboe LIST_HEAD(list); 446bd6737f1SJens Axboe 447bd6737f1SJens Axboe list_add(&rq->queuelist, &list); 448f9cd4bfeSJens Axboe e->type->ops.insert_requests(hctx, &list, at_head); 449bd6737f1SJens Axboe } else { 450bd6737f1SJens Axboe spin_lock(&ctx->lock); 451bd6737f1SJens Axboe __blk_mq_insert_request(hctx, rq, at_head); 452bd6737f1SJens Axboe spin_unlock(&ctx->lock); 453bd6737f1SJens Axboe } 454bd6737f1SJens Axboe 4550cacba6cSOmar Sandoval run: 456bd6737f1SJens Axboe if (run_queue) 457bd6737f1SJens Axboe blk_mq_run_hw_queue(hctx, async); 458bd6737f1SJens Axboe } 459bd6737f1SJens Axboe 46067cae4c9SJens Axboe void blk_mq_sched_insert_requests(struct blk_mq_hw_ctx *hctx, 461bd6737f1SJens Axboe struct blk_mq_ctx *ctx, 462bd6737f1SJens Axboe struct list_head *list, bool run_queue_async) 463bd6737f1SJens Axboe { 464f9afca4dSJens Axboe struct elevator_queue *e; 465e87eb301SMing Lei struct request_queue *q = hctx->queue; 466e87eb301SMing Lei 467e87eb301SMing Lei /* 468e87eb301SMing Lei * blk_mq_sched_insert_requests() is called from flush plug 469e87eb301SMing Lei * context only, and hold one usage counter to prevent queue 470e87eb301SMing Lei * from being released. 471e87eb301SMing Lei */ 472e87eb301SMing Lei percpu_ref_get(&q->q_usage_counter); 473f9afca4dSJens Axboe 474f9afca4dSJens Axboe e = hctx->queue->elevator; 475e42cfb1dSDamien Le Moal if (e) { 476f9cd4bfeSJens Axboe e->type->ops.insert_requests(hctx, list, false); 477e42cfb1dSDamien Le Moal } else { 4786ce3dd6eSMing Lei /* 4796ce3dd6eSMing Lei * try to issue requests directly if the hw queue isn't 4806ce3dd6eSMing Lei * busy in case of 'none' scheduler, and this way may save 4816ce3dd6eSMing Lei * us one extra enqueue & dequeue to sw queue. 4826ce3dd6eSMing Lei */ 483ef1661baSJean Sacren if (!hctx->dispatch_busy && !run_queue_async) { 4844cafe86cSMing Lei blk_mq_run_dispatch_ops(hctx->queue, 4854cafe86cSMing Lei blk_mq_try_issue_list_directly(hctx, list)); 486fd9c40f6SBart Van Assche if (list_empty(list)) 487e87eb301SMing Lei goto out; 488fd9c40f6SBart Van Assche } 489bd6737f1SJens Axboe blk_mq_insert_requests(hctx, ctx, list); 4906ce3dd6eSMing Lei } 491bd6737f1SJens Axboe 492bd6737f1SJens Axboe blk_mq_run_hw_queue(hctx, run_queue_async); 493e87eb301SMing Lei out: 494e87eb301SMing Lei percpu_ref_put(&q->q_usage_counter); 495bd6737f1SJens Axboe } 496bd6737f1SJens Axboe 497d99a6bb3SJohn Garry static int blk_mq_sched_alloc_map_and_rqs(struct request_queue *q, 4986917ff0bSOmar Sandoval struct blk_mq_hw_ctx *hctx, 4996917ff0bSOmar Sandoval unsigned int hctx_idx) 500bd166ef1SJens Axboe { 501079a2e3eSJohn Garry if (blk_mq_is_shared_tags(q->tag_set->flags)) { 502079a2e3eSJohn Garry hctx->sched_tags = q->sched_shared_tags; 503e155b0c2SJohn Garry return 0; 504e155b0c2SJohn Garry } 505e155b0c2SJohn Garry 50663064be1SJohn Garry hctx->sched_tags = blk_mq_alloc_map_and_rqs(q->tag_set, hctx_idx, 50763064be1SJohn Garry q->nr_requests); 508bd166ef1SJens Axboe 509bd166ef1SJens Axboe if (!hctx->sched_tags) 5106917ff0bSOmar Sandoval return -ENOMEM; 51163064be1SJohn Garry return 0; 512bd166ef1SJens Axboe } 513bd166ef1SJens Axboe 514079a2e3eSJohn Garry static void blk_mq_exit_sched_shared_tags(struct request_queue *queue) 515e155b0c2SJohn Garry { 516079a2e3eSJohn Garry blk_mq_free_rq_map(queue->sched_shared_tags); 517079a2e3eSJohn Garry queue->sched_shared_tags = NULL; 518e155b0c2SJohn Garry } 519e155b0c2SJohn Garry 520c3e22192SMing Lei /* called in queue's release handler, tagset has gone away */ 521e155b0c2SJohn Garry static void blk_mq_sched_tags_teardown(struct request_queue *q, unsigned int flags) 522bd166ef1SJens Axboe { 523bd166ef1SJens Axboe struct blk_mq_hw_ctx *hctx; 5244f481208SMing Lei unsigned long i; 525bd166ef1SJens Axboe 526c3e22192SMing Lei queue_for_each_hw_ctx(q, hctx, i) { 527c3e22192SMing Lei if (hctx->sched_tags) { 5288bdf7b3fSJohn Garry if (!blk_mq_is_shared_tags(flags)) 529e155b0c2SJohn Garry blk_mq_free_rq_map(hctx->sched_tags); 530c3e22192SMing Lei hctx->sched_tags = NULL; 531c3e22192SMing Lei } 532c3e22192SMing Lei } 533e155b0c2SJohn Garry 534079a2e3eSJohn Garry if (blk_mq_is_shared_tags(flags)) 535079a2e3eSJohn Garry blk_mq_exit_sched_shared_tags(q); 536bd166ef1SJens Axboe } 537d3484991SJens Axboe 538079a2e3eSJohn Garry static int blk_mq_init_sched_shared_tags(struct request_queue *queue) 539d97e594cSJohn Garry { 540d97e594cSJohn Garry struct blk_mq_tag_set *set = queue->tag_set; 541d97e594cSJohn Garry 542d97e594cSJohn Garry /* 543d97e594cSJohn Garry * Set initial depth at max so that we don't need to reallocate for 544d97e594cSJohn Garry * updating nr_requests. 545d97e594cSJohn Garry */ 546079a2e3eSJohn Garry queue->sched_shared_tags = blk_mq_alloc_map_and_rqs(set, 547e155b0c2SJohn Garry BLK_MQ_NO_HCTX_IDX, 548e155b0c2SJohn Garry MAX_SCHED_RQ); 549079a2e3eSJohn Garry if (!queue->sched_shared_tags) 550e155b0c2SJohn Garry return -ENOMEM; 551d97e594cSJohn Garry 552079a2e3eSJohn Garry blk_mq_tag_update_sched_shared_tags(queue); 553d97e594cSJohn Garry 554d97e594cSJohn Garry return 0; 555d97e594cSJohn Garry } 556d97e594cSJohn Garry 5578ed40ee3SJinlong Chen /* caller must have a reference to @e, will grab another one if successful */ 5586917ff0bSOmar Sandoval int blk_mq_init_sched(struct request_queue *q, struct elevator_type *e) 5596917ff0bSOmar Sandoval { 5604f481208SMing Lei unsigned int flags = q->tag_set->flags; 5616917ff0bSOmar Sandoval struct blk_mq_hw_ctx *hctx; 562ee056f98SOmar Sandoval struct elevator_queue *eq; 5634f481208SMing Lei unsigned long i; 5646917ff0bSOmar Sandoval int ret; 5656917ff0bSOmar Sandoval 5666917ff0bSOmar Sandoval /* 56732825c45SMing Lei * Default to double of smaller one between hw queue_depth and 128, 56832825c45SMing Lei * since we don't split into sync/async like the old code did. 56932825c45SMing Lei * Additionally, this is a per-hw queue depth. 5706917ff0bSOmar Sandoval */ 57132825c45SMing Lei q->nr_requests = 2 * min_t(unsigned int, q->tag_set->queue_depth, 572d2a27964SJohn Garry BLKDEV_DEFAULT_RQ); 5736917ff0bSOmar Sandoval 574079a2e3eSJohn Garry if (blk_mq_is_shared_tags(flags)) { 575079a2e3eSJohn Garry ret = blk_mq_init_sched_shared_tags(q); 576e155b0c2SJohn Garry if (ret) 577e155b0c2SJohn Garry return ret; 578e155b0c2SJohn Garry } 579e155b0c2SJohn Garry 5806917ff0bSOmar Sandoval queue_for_each_hw_ctx(q, hctx, i) { 581d99a6bb3SJohn Garry ret = blk_mq_sched_alloc_map_and_rqs(q, hctx, i); 5826917ff0bSOmar Sandoval if (ret) 583d99a6bb3SJohn Garry goto err_free_map_and_rqs; 584d97e594cSJohn Garry } 585d97e594cSJohn Garry 586f9cd4bfeSJens Axboe ret = e->ops.init_sched(q, e); 5876917ff0bSOmar Sandoval if (ret) 588e155b0c2SJohn Garry goto err_free_map_and_rqs; 5896917ff0bSOmar Sandoval 5905cf9c91bSChristoph Hellwig mutex_lock(&q->debugfs_mutex); 591d332ce09SOmar Sandoval blk_mq_debugfs_register_sched(q); 5925cf9c91bSChristoph Hellwig mutex_unlock(&q->debugfs_mutex); 593d332ce09SOmar Sandoval 594ee056f98SOmar Sandoval queue_for_each_hw_ctx(q, hctx, i) { 595f9cd4bfeSJens Axboe if (e->ops.init_hctx) { 596f9cd4bfeSJens Axboe ret = e->ops.init_hctx(hctx, i); 597ee056f98SOmar Sandoval if (ret) { 598ee056f98SOmar Sandoval eq = q->elevator; 5991820f4f0SJohn Garry blk_mq_sched_free_rqs(q); 600ee056f98SOmar Sandoval blk_mq_exit_sched(q, eq); 601ee056f98SOmar Sandoval kobject_put(&eq->kobj); 602ee056f98SOmar Sandoval return ret; 603ee056f98SOmar Sandoval } 604ee056f98SOmar Sandoval } 6055cf9c91bSChristoph Hellwig mutex_lock(&q->debugfs_mutex); 606d332ce09SOmar Sandoval blk_mq_debugfs_register_sched_hctx(q, hctx); 6075cf9c91bSChristoph Hellwig mutex_unlock(&q->debugfs_mutex); 608ee056f98SOmar Sandoval } 609ee056f98SOmar Sandoval 6106917ff0bSOmar Sandoval return 0; 6116917ff0bSOmar Sandoval 612d99a6bb3SJohn Garry err_free_map_and_rqs: 6131820f4f0SJohn Garry blk_mq_sched_free_rqs(q); 614e155b0c2SJohn Garry blk_mq_sched_tags_teardown(q, flags); 615e155b0c2SJohn Garry 61654d5329dSOmar Sandoval q->elevator = NULL; 6176917ff0bSOmar Sandoval return ret; 6186917ff0bSOmar Sandoval } 6196917ff0bSOmar Sandoval 620c3e22192SMing Lei /* 621c3e22192SMing Lei * called in either blk_queue_cleanup or elevator_switch, tagset 622c3e22192SMing Lei * is required for freeing requests 623c3e22192SMing Lei */ 6241820f4f0SJohn Garry void blk_mq_sched_free_rqs(struct request_queue *q) 625c3e22192SMing Lei { 626c3e22192SMing Lei struct blk_mq_hw_ctx *hctx; 6274f481208SMing Lei unsigned long i; 628c3e22192SMing Lei 629079a2e3eSJohn Garry if (blk_mq_is_shared_tags(q->tag_set->flags)) { 630079a2e3eSJohn Garry blk_mq_free_rqs(q->tag_set, q->sched_shared_tags, 631e155b0c2SJohn Garry BLK_MQ_NO_HCTX_IDX); 632e155b0c2SJohn Garry } else { 633c3e22192SMing Lei queue_for_each_hw_ctx(q, hctx, i) { 634c3e22192SMing Lei if (hctx->sched_tags) 635e155b0c2SJohn Garry blk_mq_free_rqs(q->tag_set, 636e155b0c2SJohn Garry hctx->sched_tags, i); 637e155b0c2SJohn Garry } 638c3e22192SMing Lei } 639c3e22192SMing Lei } 640c3e22192SMing Lei 64154d5329dSOmar Sandoval void blk_mq_exit_sched(struct request_queue *q, struct elevator_queue *e) 64254d5329dSOmar Sandoval { 643ee056f98SOmar Sandoval struct blk_mq_hw_ctx *hctx; 6444f481208SMing Lei unsigned long i; 645f0c1c4d2SMing Lei unsigned int flags = 0; 646ee056f98SOmar Sandoval 647ee056f98SOmar Sandoval queue_for_each_hw_ctx(q, hctx, i) { 6485cf9c91bSChristoph Hellwig mutex_lock(&q->debugfs_mutex); 649d332ce09SOmar Sandoval blk_mq_debugfs_unregister_sched_hctx(hctx); 6505cf9c91bSChristoph Hellwig mutex_unlock(&q->debugfs_mutex); 6515cf9c91bSChristoph Hellwig 652f9cd4bfeSJens Axboe if (e->type->ops.exit_hctx && hctx->sched_data) { 653f9cd4bfeSJens Axboe e->type->ops.exit_hctx(hctx, i); 654ee056f98SOmar Sandoval hctx->sched_data = NULL; 655ee056f98SOmar Sandoval } 656f0c1c4d2SMing Lei flags = hctx->flags; 657ee056f98SOmar Sandoval } 6585cf9c91bSChristoph Hellwig 6595cf9c91bSChristoph Hellwig mutex_lock(&q->debugfs_mutex); 660d332ce09SOmar Sandoval blk_mq_debugfs_unregister_sched(q); 6615cf9c91bSChristoph Hellwig mutex_unlock(&q->debugfs_mutex); 6625cf9c91bSChristoph Hellwig 663f9cd4bfeSJens Axboe if (e->type->ops.exit_sched) 664f9cd4bfeSJens Axboe e->type->ops.exit_sched(e); 665e155b0c2SJohn Garry blk_mq_sched_tags_teardown(q, flags); 66654d5329dSOmar Sandoval q->elevator = NULL; 66754d5329dSOmar Sandoval } 668