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 21e2b3fa5aSDamien Le Moal void blk_mq_sched_assign_ioc(struct request *rq) 22bd166ef1SJens Axboe { 2344e8c2bfSChristoph Hellwig struct request_queue *q = rq->q; 240c62bff1SJens Axboe struct io_context *ioc; 25bd166ef1SJens Axboe struct io_cq *icq; 26bd166ef1SJens Axboe 270c62bff1SJens Axboe /* 280c62bff1SJens Axboe * May not have an IO context if it's a passthrough request 290c62bff1SJens Axboe */ 300c62bff1SJens Axboe ioc = current->io_context; 310c62bff1SJens Axboe if (!ioc) 320c62bff1SJens Axboe return; 330c62bff1SJens Axboe 340d945c1fSChristoph Hellwig spin_lock_irq(&q->queue_lock); 35bd166ef1SJens Axboe icq = ioc_lookup_icq(ioc, q); 360d945c1fSChristoph Hellwig spin_unlock_irq(&q->queue_lock); 37bd166ef1SJens Axboe 38bd166ef1SJens Axboe if (!icq) { 39bd166ef1SJens Axboe icq = ioc_create_icq(ioc, q, GFP_ATOMIC); 40bd166ef1SJens Axboe if (!icq) 41bd166ef1SJens Axboe return; 42bd166ef1SJens Axboe } 43ea511e3cSChristoph Hellwig get_io_context(icq->ioc); 4444e8c2bfSChristoph Hellwig rq->elv.icq = icq; 45bd166ef1SJens Axboe } 46bd166ef1SJens Axboe 478e8320c9SJens Axboe /* 488e8320c9SJens Axboe * Mark a hardware queue as needing a restart. For shared queues, maintain 498e8320c9SJens Axboe * a count of how many hardware queues are marked for restart. 508e8320c9SJens Axboe */ 517211aef8SDamien Le Moal void blk_mq_sched_mark_restart_hctx(struct blk_mq_hw_ctx *hctx) 528e8320c9SJens Axboe { 538e8320c9SJens Axboe if (test_bit(BLK_MQ_S_SCHED_RESTART, &hctx->state)) 548e8320c9SJens Axboe return; 558e8320c9SJens Axboe 568e8320c9SJens Axboe set_bit(BLK_MQ_S_SCHED_RESTART, &hctx->state); 578e8320c9SJens Axboe } 587211aef8SDamien Le Moal EXPORT_SYMBOL_GPL(blk_mq_sched_mark_restart_hctx); 598e8320c9SJens Axboe 6097889f9aSMing Lei void blk_mq_sched_restart(struct blk_mq_hw_ctx *hctx) 618e8320c9SJens Axboe { 628e8320c9SJens Axboe if (!test_bit(BLK_MQ_S_SCHED_RESTART, &hctx->state)) 6397889f9aSMing Lei return; 648e8320c9SJens Axboe clear_bit(BLK_MQ_S_SCHED_RESTART, &hctx->state); 658e8320c9SJens Axboe 66d7d8535fSMing Lei /* 67d7d8535fSMing Lei * Order clearing SCHED_RESTART and list_empty_careful(&hctx->dispatch) 68d7d8535fSMing Lei * in blk_mq_run_hw_queue(). Its pair is the barrier in 69d7d8535fSMing Lei * blk_mq_dispatch_rq_list(). So dispatch code won't see SCHED_RESTART, 70d7d8535fSMing Lei * meantime new request added to hctx->dispatch is missed to check in 71d7d8535fSMing Lei * blk_mq_run_hw_queue(). 72d7d8535fSMing Lei */ 73d7d8535fSMing Lei smp_mb(); 74d7d8535fSMing Lei 7597889f9aSMing Lei blk_mq_run_hw_queue(hctx, true); 768e8320c9SJens Axboe } 778e8320c9SJens Axboe 78*4f0f586bSSami Tolvanen static int sched_rq_cmp(void *priv, const struct list_head *a, 79*4f0f586bSSami Tolvanen const struct list_head *b) 806e6fcbc2SMing Lei { 816e6fcbc2SMing Lei struct request *rqa = container_of(a, struct request, queuelist); 826e6fcbc2SMing Lei struct request *rqb = container_of(b, struct request, queuelist); 836e6fcbc2SMing Lei 846e6fcbc2SMing Lei return rqa->mq_hctx > rqb->mq_hctx; 856e6fcbc2SMing Lei } 866e6fcbc2SMing Lei 876e6fcbc2SMing Lei static bool blk_mq_dispatch_hctx_list(struct list_head *rq_list) 886e6fcbc2SMing Lei { 896e6fcbc2SMing Lei struct blk_mq_hw_ctx *hctx = 906e6fcbc2SMing Lei list_first_entry(rq_list, struct request, queuelist)->mq_hctx; 916e6fcbc2SMing Lei struct request *rq; 926e6fcbc2SMing Lei LIST_HEAD(hctx_list); 936e6fcbc2SMing Lei unsigned int count = 0; 946e6fcbc2SMing Lei 956e6fcbc2SMing Lei list_for_each_entry(rq, rq_list, queuelist) { 966e6fcbc2SMing Lei if (rq->mq_hctx != hctx) { 976e6fcbc2SMing Lei list_cut_before(&hctx_list, rq_list, &rq->queuelist); 986e6fcbc2SMing Lei goto dispatch; 996e6fcbc2SMing Lei } 1006e6fcbc2SMing Lei count++; 1016e6fcbc2SMing Lei } 1026e6fcbc2SMing Lei list_splice_tail_init(rq_list, &hctx_list); 1036e6fcbc2SMing Lei 1046e6fcbc2SMing Lei dispatch: 105106e71c5SBaolin Wang return blk_mq_dispatch_rq_list(hctx, &hctx_list, count); 1066e6fcbc2SMing Lei } 1076e6fcbc2SMing Lei 108a0823421SDouglas Anderson #define BLK_MQ_BUDGET_DELAY 3 /* ms units */ 109a0823421SDouglas Anderson 1101f460b63SMing Lei /* 1111f460b63SMing Lei * Only SCSI implements .get_budget and .put_budget, and SCSI restarts 1121f460b63SMing Lei * its queue by itself in its completion handler, so we don't need to 1131f460b63SMing Lei * restart queue if .get_budget() returns BLK_STS_NO_RESOURCE. 11428d65729SSalman Qazi * 11528d65729SSalman Qazi * Returns -EAGAIN if hctx->dispatch was found non-empty and run_work has to 11628d65729SSalman Qazi * be run again. This is necessary to avoid starving flushes. 1171f460b63SMing Lei */ 1186e6fcbc2SMing Lei static int __blk_mq_do_dispatch_sched(struct blk_mq_hw_ctx *hctx) 119caf8eb0dSMing Lei { 120caf8eb0dSMing Lei struct request_queue *q = hctx->queue; 121caf8eb0dSMing Lei struct elevator_queue *e = q->elevator; 1226e6fcbc2SMing Lei bool multi_hctxs = false, run_queue = false; 1236e6fcbc2SMing Lei bool dispatched = false, busy = false; 1246e6fcbc2SMing Lei unsigned int max_dispatch; 125caf8eb0dSMing Lei LIST_HEAD(rq_list); 1266e6fcbc2SMing Lei int count = 0; 1276e6fcbc2SMing Lei 1286e6fcbc2SMing Lei if (hctx->dispatch_busy) 1296e6fcbc2SMing Lei max_dispatch = 1; 1306e6fcbc2SMing Lei else 1316e6fcbc2SMing Lei max_dispatch = hctx->queue->nr_requests; 132caf8eb0dSMing Lei 133445874e8SMing Lei do { 1346e6fcbc2SMing Lei struct request *rq; 1356e6fcbc2SMing Lei 136f9cd4bfeSJens Axboe if (e->type->ops.has_work && !e->type->ops.has_work(hctx)) 137caf8eb0dSMing Lei break; 138de148297SMing Lei 13928d65729SSalman Qazi if (!list_empty_careful(&hctx->dispatch)) { 1406e6fcbc2SMing Lei busy = true; 14128d65729SSalman Qazi break; 14228d65729SSalman Qazi } 14328d65729SSalman Qazi 14465c76369SMing Lei if (!blk_mq_get_dispatch_budget(q)) 1451f460b63SMing Lei break; 146de148297SMing Lei 147f9cd4bfeSJens Axboe rq = e->type->ops.dispatch_request(hctx); 148de148297SMing Lei if (!rq) { 14965c76369SMing Lei blk_mq_put_dispatch_budget(q); 150a0823421SDouglas Anderson /* 151a0823421SDouglas Anderson * We're releasing without dispatching. Holding the 152a0823421SDouglas Anderson * budget could have blocked any "hctx"s with the 153a0823421SDouglas Anderson * same queue and if we didn't dispatch then there's 154a0823421SDouglas Anderson * no guarantee anyone will kick the queue. Kick it 155a0823421SDouglas Anderson * ourselves. 156a0823421SDouglas Anderson */ 1576e6fcbc2SMing Lei run_queue = true; 158de148297SMing Lei break; 159caf8eb0dSMing Lei } 160caf8eb0dSMing Lei 161de148297SMing Lei /* 162de148297SMing Lei * Now this rq owns the budget which has to be released 163de148297SMing Lei * if this rq won't be queued to driver via .queue_rq() 164de148297SMing Lei * in blk_mq_dispatch_rq_list(). 165de148297SMing Lei */ 1666e6fcbc2SMing Lei list_add_tail(&rq->queuelist, &rq_list); 1676e6fcbc2SMing Lei if (rq->mq_hctx != hctx) 1686e6fcbc2SMing Lei multi_hctxs = true; 1696e6fcbc2SMing Lei } while (++count < max_dispatch); 1706e6fcbc2SMing Lei 1716e6fcbc2SMing Lei if (!count) { 1726e6fcbc2SMing Lei if (run_queue) 1736e6fcbc2SMing Lei blk_mq_delay_run_hw_queues(q, BLK_MQ_BUDGET_DELAY); 1746e6fcbc2SMing Lei } else if (multi_hctxs) { 1756e6fcbc2SMing Lei /* 1766e6fcbc2SMing Lei * Requests from different hctx may be dequeued from some 1776e6fcbc2SMing Lei * schedulers, such as bfq and deadline. 1786e6fcbc2SMing Lei * 1796e6fcbc2SMing Lei * Sort the requests in the list according to their hctx, 1806e6fcbc2SMing Lei * dispatch batching requests from same hctx at a time. 1816e6fcbc2SMing Lei */ 1826e6fcbc2SMing Lei list_sort(NULL, &rq_list, sched_rq_cmp); 1836e6fcbc2SMing Lei do { 1846e6fcbc2SMing Lei dispatched |= blk_mq_dispatch_hctx_list(&rq_list); 1856e6fcbc2SMing Lei } while (!list_empty(&rq_list)); 1866e6fcbc2SMing Lei } else { 1876e6fcbc2SMing Lei dispatched = blk_mq_dispatch_rq_list(hctx, &rq_list, count); 1886e6fcbc2SMing Lei } 1896e6fcbc2SMing Lei 1906e6fcbc2SMing Lei if (busy) 1916e6fcbc2SMing Lei return -EAGAIN; 1926e6fcbc2SMing Lei return !!dispatched; 1936e6fcbc2SMing Lei } 1946e6fcbc2SMing Lei 1956e6fcbc2SMing Lei static int blk_mq_do_dispatch_sched(struct blk_mq_hw_ctx *hctx) 1966e6fcbc2SMing Lei { 1976e6fcbc2SMing Lei int ret; 1986e6fcbc2SMing Lei 1996e6fcbc2SMing Lei do { 2006e6fcbc2SMing Lei ret = __blk_mq_do_dispatch_sched(hctx); 2016e6fcbc2SMing Lei } while (ret == 1); 20228d65729SSalman Qazi 20328d65729SSalman Qazi return ret; 204de148297SMing Lei } 205de148297SMing Lei 206b347689fSMing Lei static struct blk_mq_ctx *blk_mq_next_ctx(struct blk_mq_hw_ctx *hctx, 207b347689fSMing Lei struct blk_mq_ctx *ctx) 208b347689fSMing Lei { 209f31967f0SJens Axboe unsigned short idx = ctx->index_hw[hctx->type]; 210b347689fSMing Lei 211b347689fSMing Lei if (++idx == hctx->nr_ctx) 212b347689fSMing Lei idx = 0; 213b347689fSMing Lei 214b347689fSMing Lei return hctx->ctxs[idx]; 215b347689fSMing Lei } 216b347689fSMing Lei 2171f460b63SMing Lei /* 2181f460b63SMing Lei * Only SCSI implements .get_budget and .put_budget, and SCSI restarts 2191f460b63SMing Lei * its queue by itself in its completion handler, so we don't need to 2201f460b63SMing Lei * restart queue if .get_budget() returns BLK_STS_NO_RESOURCE. 22128d65729SSalman Qazi * 22228d65729SSalman Qazi * Returns -EAGAIN if hctx->dispatch was found non-empty and run_work has to 223c4aecaa2SRandy Dunlap * be run again. This is necessary to avoid starving flushes. 2241f460b63SMing Lei */ 22528d65729SSalman Qazi static int blk_mq_do_dispatch_ctx(struct blk_mq_hw_ctx *hctx) 226b347689fSMing Lei { 227b347689fSMing Lei struct request_queue *q = hctx->queue; 228b347689fSMing Lei LIST_HEAD(rq_list); 229b347689fSMing Lei struct blk_mq_ctx *ctx = READ_ONCE(hctx->dispatch_from); 23028d65729SSalman Qazi int ret = 0; 231b347689fSMing Lei struct request *rq; 232b347689fSMing Lei 233445874e8SMing Lei do { 23428d65729SSalman Qazi if (!list_empty_careful(&hctx->dispatch)) { 23528d65729SSalman Qazi ret = -EAGAIN; 23628d65729SSalman Qazi break; 23728d65729SSalman Qazi } 23828d65729SSalman Qazi 239b347689fSMing Lei if (!sbitmap_any_bit_set(&hctx->ctx_map)) 240b347689fSMing Lei break; 241b347689fSMing Lei 24265c76369SMing Lei if (!blk_mq_get_dispatch_budget(q)) 2431f460b63SMing Lei break; 244b347689fSMing Lei 245b347689fSMing Lei rq = blk_mq_dequeue_from_ctx(hctx, ctx); 246b347689fSMing Lei if (!rq) { 24765c76369SMing Lei blk_mq_put_dispatch_budget(q); 248a0823421SDouglas Anderson /* 249a0823421SDouglas Anderson * We're releasing without dispatching. Holding the 250a0823421SDouglas Anderson * budget could have blocked any "hctx"s with the 251a0823421SDouglas Anderson * same queue and if we didn't dispatch then there's 252a0823421SDouglas Anderson * no guarantee anyone will kick the queue. Kick it 253a0823421SDouglas Anderson * ourselves. 254a0823421SDouglas Anderson */ 255a0823421SDouglas Anderson blk_mq_delay_run_hw_queues(q, BLK_MQ_BUDGET_DELAY); 256b347689fSMing Lei break; 257b347689fSMing Lei } 258b347689fSMing Lei 259b347689fSMing Lei /* 260b347689fSMing Lei * Now this rq owns the budget which has to be released 261b347689fSMing Lei * if this rq won't be queued to driver via .queue_rq() 262b347689fSMing Lei * in blk_mq_dispatch_rq_list(). 263b347689fSMing Lei */ 264b347689fSMing Lei list_add(&rq->queuelist, &rq_list); 265b347689fSMing Lei 266b347689fSMing Lei /* round robin for fair dispatch */ 267b347689fSMing Lei ctx = blk_mq_next_ctx(hctx, rq->mq_ctx); 268b347689fSMing Lei 2691fd40b5eSMing Lei } while (blk_mq_dispatch_rq_list(rq->mq_hctx, &rq_list, 1)); 270b347689fSMing Lei 271b347689fSMing Lei WRITE_ONCE(hctx->dispatch_from, ctx); 27228d65729SSalman Qazi return ret; 273b347689fSMing Lei } 274b347689fSMing Lei 275e1b586f2SZheng Bin static int __blk_mq_sched_dispatch_requests(struct blk_mq_hw_ctx *hctx) 276bd166ef1SJens Axboe { 27781380ca1SOmar Sandoval struct request_queue *q = hctx->queue; 27881380ca1SOmar Sandoval struct elevator_queue *e = q->elevator; 279f9cd4bfeSJens Axboe const bool has_sched_dispatch = e && e->type->ops.dispatch_request; 28028d65729SSalman Qazi int ret = 0; 281bd166ef1SJens Axboe LIST_HEAD(rq_list); 282bd166ef1SJens Axboe 283bd166ef1SJens Axboe /* 284bd166ef1SJens Axboe * If we have previous entries on our dispatch list, grab them first for 285bd166ef1SJens Axboe * more fair dispatch. 286bd166ef1SJens Axboe */ 287bd166ef1SJens Axboe if (!list_empty_careful(&hctx->dispatch)) { 288bd166ef1SJens Axboe spin_lock(&hctx->lock); 289bd166ef1SJens Axboe if (!list_empty(&hctx->dispatch)) 290bd166ef1SJens Axboe list_splice_init(&hctx->dispatch, &rq_list); 291bd166ef1SJens Axboe spin_unlock(&hctx->lock); 292bd166ef1SJens Axboe } 293bd166ef1SJens Axboe 294bd166ef1SJens Axboe /* 295bd166ef1SJens Axboe * Only ask the scheduler for requests, if we didn't have residual 296bd166ef1SJens Axboe * requests from the dispatch list. This is to avoid the case where 297bd166ef1SJens Axboe * we only ever dispatch a fraction of the requests available because 298bd166ef1SJens Axboe * of low device queue depth. Once we pull requests out of the IO 299bd166ef1SJens Axboe * scheduler, we can no longer merge or sort them. So it's best to 300bd166ef1SJens Axboe * leave them there for as long as we can. Mark the hw queue as 301bd166ef1SJens Axboe * needing a restart in that case. 302caf8eb0dSMing Lei * 3035e3d02bbSMing Lei * We want to dispatch from the scheduler if there was nothing 3045e3d02bbSMing Lei * on the dispatch list or we were able to dispatch from the 3055e3d02bbSMing Lei * dispatch list. 30664765a75SJens Axboe */ 307caf8eb0dSMing Lei if (!list_empty(&rq_list)) { 308caf8eb0dSMing Lei blk_mq_sched_mark_restart_hctx(hctx); 3091fd40b5eSMing Lei if (blk_mq_dispatch_rq_list(hctx, &rq_list, 0)) { 310b347689fSMing Lei if (has_sched_dispatch) 31128d65729SSalman Qazi ret = blk_mq_do_dispatch_sched(hctx); 312b347689fSMing Lei else 31328d65729SSalman Qazi ret = blk_mq_do_dispatch_ctx(hctx); 314b347689fSMing Lei } 315caf8eb0dSMing Lei } else if (has_sched_dispatch) { 31628d65729SSalman Qazi ret = blk_mq_do_dispatch_sched(hctx); 3176e768717SMing Lei } else if (hctx->dispatch_busy) { 3186e768717SMing Lei /* dequeue request one by one from sw queue if queue is busy */ 31928d65729SSalman Qazi ret = blk_mq_do_dispatch_ctx(hctx); 320caf8eb0dSMing Lei } else { 321caf8eb0dSMing Lei blk_mq_flush_busy_ctxs(hctx, &rq_list); 3221fd40b5eSMing Lei blk_mq_dispatch_rq_list(hctx, &rq_list, 0); 323c13660a0SJens Axboe } 32428d65729SSalman Qazi 32528d65729SSalman Qazi return ret; 32628d65729SSalman Qazi } 32728d65729SSalman Qazi 32828d65729SSalman Qazi void blk_mq_sched_dispatch_requests(struct blk_mq_hw_ctx *hctx) 32928d65729SSalman Qazi { 33028d65729SSalman Qazi struct request_queue *q = hctx->queue; 33128d65729SSalman Qazi 33228d65729SSalman Qazi /* RCU or SRCU read lock is needed before checking quiesced flag */ 33328d65729SSalman Qazi if (unlikely(blk_mq_hctx_stopped(hctx) || blk_queue_quiesced(q))) 33428d65729SSalman Qazi return; 33528d65729SSalman Qazi 33628d65729SSalman Qazi hctx->run++; 33728d65729SSalman Qazi 33828d65729SSalman Qazi /* 33928d65729SSalman Qazi * A return of -EAGAIN is an indication that hctx->dispatch is not 34028d65729SSalman Qazi * empty and we must run again in order to avoid starving flushes. 34128d65729SSalman Qazi */ 34228d65729SSalman Qazi if (__blk_mq_sched_dispatch_requests(hctx) == -EAGAIN) { 34328d65729SSalman Qazi if (__blk_mq_sched_dispatch_requests(hctx) == -EAGAIN) 34428d65729SSalman Qazi blk_mq_run_hw_queue(hctx, true); 34528d65729SSalman Qazi } 346bd166ef1SJens Axboe } 347bd166ef1SJens Axboe 34814ccb66bSChristoph Hellwig bool __blk_mq_sched_bio_merge(struct request_queue *q, struct bio *bio, 34914ccb66bSChristoph Hellwig unsigned int nr_segs) 350bd166ef1SJens Axboe { 351bd166ef1SJens Axboe struct elevator_queue *e = q->elevator; 352bd166ef1SJens Axboe struct blk_mq_ctx *ctx = blk_mq_get_ctx(q); 3538ccdf4a3SJianchao Wang struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(q, bio->bi_opf, ctx); 3549bddeb2aSMing Lei bool ret = false; 355c16d6b5aSMing Lei enum hctx_type type; 356bd166ef1SJens Axboe 357c05f4220SBart Van Assche if (e && e->type->ops.bio_merge) 35814ccb66bSChristoph Hellwig return e->type->ops.bio_merge(hctx, bio, nr_segs); 359bd166ef1SJens Axboe 360c16d6b5aSMing Lei type = hctx->type; 361cdfcef9eSBaolin Wang if (!(hctx->flags & BLK_MQ_F_SHOULD_MERGE) || 362cdfcef9eSBaolin Wang list_empty_careful(&ctx->rq_lists[type])) 363cdfcef9eSBaolin Wang return false; 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 */ 372cdfcef9eSBaolin Wang if (blk_bio_list_merge(q, &ctx->rq_lists[type], bio, nr_segs)) { 373cdfcef9eSBaolin Wang ctx->rq_merged++; 374cdfcef9eSBaolin Wang ret = true; 3759bddeb2aSMing Lei } 3769bddeb2aSMing Lei 377cdfcef9eSBaolin Wang spin_unlock(&ctx->lock); 378cdfcef9eSBaolin Wang 3799bddeb2aSMing Lei return ret; 380bd166ef1SJens Axboe } 381bd166ef1SJens Axboe 382bd166ef1SJens Axboe bool blk_mq_sched_try_insert_merge(struct request_queue *q, struct request *rq) 383bd166ef1SJens Axboe { 384bd166ef1SJens Axboe return rq_mergeable(rq) && elv_attempt_insert_merge(q, rq); 385bd166ef1SJens Axboe } 386bd166ef1SJens Axboe EXPORT_SYMBOL_GPL(blk_mq_sched_try_insert_merge); 387bd166ef1SJens Axboe 3880cacba6cSOmar Sandoval static bool blk_mq_sched_bypass_insert(struct blk_mq_hw_ctx *hctx, 3890cacba6cSOmar Sandoval struct request *rq) 390bd166ef1SJens Axboe { 39101e99aecSMing Lei /* 39201e99aecSMing Lei * dispatch flush and passthrough rq directly 39301e99aecSMing Lei * 39401e99aecSMing Lei * passthrough request has to be added to hctx->dispatch directly. 39501e99aecSMing Lei * For some reason, device may be in one situation which can't 39601e99aecSMing Lei * handle FS request, so STS_RESOURCE is always returned and the 39701e99aecSMing Lei * FS request will be added to hctx->dispatch. However passthrough 39801e99aecSMing Lei * request may be required at that time for fixing the problem. If 39901e99aecSMing Lei * passthrough request is added to scheduler queue, there isn't any 40001e99aecSMing Lei * chance to dispatch it given we prioritize requests in hctx->dispatch. 40101e99aecSMing Lei */ 40201e99aecSMing Lei if ((rq->rq_flags & RQF_FLUSH_SEQ) || blk_rq_is_passthrough(rq)) 403bd166ef1SJens Axboe return true; 404bd166ef1SJens Axboe 405a6a252e6SMing Lei return false; 406a6a252e6SMing Lei } 407a6a252e6SMing Lei 408bd6737f1SJens Axboe void blk_mq_sched_insert_request(struct request *rq, bool at_head, 4099e97d295SMike Snitzer bool run_queue, bool async) 410bd6737f1SJens Axboe { 411bd6737f1SJens Axboe struct request_queue *q = rq->q; 412bd6737f1SJens Axboe struct elevator_queue *e = q->elevator; 413bd6737f1SJens Axboe struct blk_mq_ctx *ctx = rq->mq_ctx; 414ea4f995eSJens Axboe struct blk_mq_hw_ctx *hctx = rq->mq_hctx; 415bd6737f1SJens Axboe 416e44a6a23SXianting Tian WARN_ON(e && (rq->tag != BLK_MQ_NO_TAG)); 417923218f6SMing Lei 4185218e12eSJean Delvare if (blk_mq_sched_bypass_insert(hctx, rq)) { 419cc3200eaSMing Lei /* 420cc3200eaSMing Lei * Firstly normal IO request is inserted to scheduler queue or 421cc3200eaSMing Lei * sw queue, meantime we add flush request to dispatch queue( 422cc3200eaSMing Lei * hctx->dispatch) directly and there is at most one in-flight 423cc3200eaSMing Lei * flush request for each hw queue, so it doesn't matter to add 424cc3200eaSMing Lei * flush request to tail or front of the dispatch queue. 425cc3200eaSMing Lei * 426cc3200eaSMing Lei * Secondly in case of NCQ, flush request belongs to non-NCQ 427cc3200eaSMing Lei * command, and queueing it will fail when there is any 428cc3200eaSMing Lei * in-flight normal IO request(NCQ command). When adding flush 429cc3200eaSMing Lei * rq to the front of hctx->dispatch, it is easier to introduce 430cc3200eaSMing Lei * extra time to flush rq's latency because of S_SCHED_RESTART 431cc3200eaSMing Lei * compared with adding to the tail of dispatch queue, then 432cc3200eaSMing Lei * chance of flush merge is increased, and less flush requests 433cc3200eaSMing Lei * will be issued to controller. It is observed that ~10% time 434cc3200eaSMing Lei * is saved in blktests block/004 on disk attached to AHCI/NCQ 435cc3200eaSMing Lei * drive when adding flush rq to the front of hctx->dispatch. 436cc3200eaSMing Lei * 437cc3200eaSMing Lei * Simply queue flush rq to the front of hctx->dispatch so that 438cc3200eaSMing Lei * intensive flush workloads can benefit in case of NCQ HW. 439cc3200eaSMing Lei */ 440cc3200eaSMing Lei at_head = (rq->rq_flags & RQF_FLUSH_SEQ) ? true : at_head; 44101e99aecSMing Lei blk_mq_request_bypass_insert(rq, at_head, false); 4420cacba6cSOmar Sandoval goto run; 44301e99aecSMing Lei } 4440cacba6cSOmar Sandoval 445f9cd4bfeSJens Axboe if (e && e->type->ops.insert_requests) { 446bd6737f1SJens Axboe LIST_HEAD(list); 447bd6737f1SJens Axboe 448bd6737f1SJens Axboe list_add(&rq->queuelist, &list); 449f9cd4bfeSJens Axboe e->type->ops.insert_requests(hctx, &list, at_head); 450bd6737f1SJens Axboe } else { 451bd6737f1SJens Axboe spin_lock(&ctx->lock); 452bd6737f1SJens Axboe __blk_mq_insert_request(hctx, rq, at_head); 453bd6737f1SJens Axboe spin_unlock(&ctx->lock); 454bd6737f1SJens Axboe } 455bd6737f1SJens Axboe 4560cacba6cSOmar Sandoval run: 457bd6737f1SJens Axboe if (run_queue) 458bd6737f1SJens Axboe blk_mq_run_hw_queue(hctx, async); 459bd6737f1SJens Axboe } 460bd6737f1SJens Axboe 46167cae4c9SJens Axboe void blk_mq_sched_insert_requests(struct blk_mq_hw_ctx *hctx, 462bd6737f1SJens Axboe struct blk_mq_ctx *ctx, 463bd6737f1SJens Axboe struct list_head *list, bool run_queue_async) 464bd6737f1SJens Axboe { 465f9afca4dSJens Axboe struct elevator_queue *e; 466e87eb301SMing Lei struct request_queue *q = hctx->queue; 467e87eb301SMing Lei 468e87eb301SMing Lei /* 469e87eb301SMing Lei * blk_mq_sched_insert_requests() is called from flush plug 470e87eb301SMing Lei * context only, and hold one usage counter to prevent queue 471e87eb301SMing Lei * from being released. 472e87eb301SMing Lei */ 473e87eb301SMing Lei percpu_ref_get(&q->q_usage_counter); 474f9afca4dSJens Axboe 475f9afca4dSJens Axboe e = hctx->queue->elevator; 476f9cd4bfeSJens Axboe if (e && e->type->ops.insert_requests) 477f9cd4bfeSJens Axboe e->type->ops.insert_requests(hctx, list, false); 4786ce3dd6eSMing Lei else { 4796ce3dd6eSMing Lei /* 4806ce3dd6eSMing Lei * try to issue requests directly if the hw queue isn't 4816ce3dd6eSMing Lei * busy in case of 'none' scheduler, and this way may save 4826ce3dd6eSMing Lei * us one extra enqueue & dequeue to sw queue. 4836ce3dd6eSMing Lei */ 484fd9c40f6SBart Van Assche if (!hctx->dispatch_busy && !e && !run_queue_async) { 4856ce3dd6eSMing 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 497bd166ef1SJens Axboe static void blk_mq_sched_free_tags(struct blk_mq_tag_set *set, 498bd166ef1SJens Axboe struct blk_mq_hw_ctx *hctx, 499bd166ef1SJens Axboe unsigned int hctx_idx) 500bd166ef1SJens Axboe { 50132bc15afSJohn Garry unsigned int flags = set->flags & ~BLK_MQ_F_TAG_HCTX_SHARED; 5021c0706a7SJohn Garry 503bd166ef1SJens Axboe if (hctx->sched_tags) { 504bd166ef1SJens Axboe blk_mq_free_rqs(set, hctx->sched_tags, hctx_idx); 5051c0706a7SJohn Garry blk_mq_free_rq_map(hctx->sched_tags, flags); 506bd166ef1SJens Axboe hctx->sched_tags = NULL; 507bd166ef1SJens Axboe } 508bd166ef1SJens Axboe } 509bd166ef1SJens Axboe 5106917ff0bSOmar Sandoval static int blk_mq_sched_alloc_tags(struct request_queue *q, 5116917ff0bSOmar Sandoval struct blk_mq_hw_ctx *hctx, 5126917ff0bSOmar Sandoval unsigned int hctx_idx) 513bd166ef1SJens Axboe { 514bd166ef1SJens Axboe struct blk_mq_tag_set *set = q->tag_set; 51532bc15afSJohn Garry /* Clear HCTX_SHARED so tags are init'ed */ 51632bc15afSJohn Garry unsigned int flags = set->flags & ~BLK_MQ_F_TAG_HCTX_SHARED; 5176917ff0bSOmar Sandoval int ret; 518bd166ef1SJens Axboe 5196917ff0bSOmar Sandoval hctx->sched_tags = blk_mq_alloc_rq_map(set, hctx_idx, q->nr_requests, 5201c0706a7SJohn Garry set->reserved_tags, flags); 521bd166ef1SJens Axboe if (!hctx->sched_tags) 5226917ff0bSOmar Sandoval return -ENOMEM; 5236917ff0bSOmar Sandoval 5246917ff0bSOmar Sandoval ret = blk_mq_alloc_rqs(set, hctx->sched_tags, hctx_idx, q->nr_requests); 5256917ff0bSOmar Sandoval if (ret) 5266917ff0bSOmar Sandoval blk_mq_sched_free_tags(set, hctx, hctx_idx); 527bd166ef1SJens Axboe 528bd166ef1SJens Axboe return ret; 529bd166ef1SJens Axboe } 530bd166ef1SJens Axboe 531c3e22192SMing Lei /* called in queue's release handler, tagset has gone away */ 53254d5329dSOmar Sandoval static void blk_mq_sched_tags_teardown(struct request_queue *q) 533bd166ef1SJens Axboe { 534bd166ef1SJens Axboe struct blk_mq_hw_ctx *hctx; 535bd166ef1SJens Axboe int i; 536bd166ef1SJens Axboe 537c3e22192SMing Lei queue_for_each_hw_ctx(q, hctx, i) { 53832bc15afSJohn Garry /* Clear HCTX_SHARED so tags are freed */ 53932bc15afSJohn Garry unsigned int flags = hctx->flags & ~BLK_MQ_F_TAG_HCTX_SHARED; 5401c0706a7SJohn Garry 541c3e22192SMing Lei if (hctx->sched_tags) { 5421c0706a7SJohn Garry blk_mq_free_rq_map(hctx->sched_tags, flags); 543c3e22192SMing Lei hctx->sched_tags = NULL; 544c3e22192SMing Lei } 545c3e22192SMing Lei } 546bd166ef1SJens Axboe } 547d3484991SJens Axboe 5486917ff0bSOmar Sandoval int blk_mq_init_sched(struct request_queue *q, struct elevator_type *e) 5496917ff0bSOmar Sandoval { 5506917ff0bSOmar Sandoval struct blk_mq_hw_ctx *hctx; 551ee056f98SOmar Sandoval struct elevator_queue *eq; 5526917ff0bSOmar Sandoval unsigned int i; 5536917ff0bSOmar Sandoval int ret; 5546917ff0bSOmar Sandoval 5556917ff0bSOmar Sandoval if (!e) { 5566917ff0bSOmar Sandoval q->elevator = NULL; 55732a50fabSMing Lei q->nr_requests = q->tag_set->queue_depth; 5586917ff0bSOmar Sandoval return 0; 5596917ff0bSOmar Sandoval } 5606917ff0bSOmar Sandoval 5616917ff0bSOmar Sandoval /* 56232825c45SMing Lei * Default to double of smaller one between hw queue_depth and 128, 56332825c45SMing Lei * since we don't split into sync/async like the old code did. 56432825c45SMing Lei * Additionally, this is a per-hw queue depth. 5656917ff0bSOmar Sandoval */ 56632825c45SMing Lei q->nr_requests = 2 * min_t(unsigned int, q->tag_set->queue_depth, 56732825c45SMing Lei BLKDEV_MAX_RQ); 5686917ff0bSOmar Sandoval 5696917ff0bSOmar Sandoval queue_for_each_hw_ctx(q, hctx, i) { 5706917ff0bSOmar Sandoval ret = blk_mq_sched_alloc_tags(q, hctx, i); 5716917ff0bSOmar Sandoval if (ret) 5726917ff0bSOmar Sandoval goto err; 5736917ff0bSOmar Sandoval } 5746917ff0bSOmar Sandoval 575f9cd4bfeSJens Axboe ret = e->ops.init_sched(q, e); 5766917ff0bSOmar Sandoval if (ret) 5776917ff0bSOmar Sandoval goto err; 5786917ff0bSOmar Sandoval 579d332ce09SOmar Sandoval blk_mq_debugfs_register_sched(q); 580d332ce09SOmar Sandoval 581ee056f98SOmar Sandoval queue_for_each_hw_ctx(q, hctx, i) { 582f9cd4bfeSJens Axboe if (e->ops.init_hctx) { 583f9cd4bfeSJens Axboe ret = e->ops.init_hctx(hctx, i); 584ee056f98SOmar Sandoval if (ret) { 585ee056f98SOmar Sandoval eq = q->elevator; 586c3e22192SMing Lei blk_mq_sched_free_requests(q); 587ee056f98SOmar Sandoval blk_mq_exit_sched(q, eq); 588ee056f98SOmar Sandoval kobject_put(&eq->kobj); 589ee056f98SOmar Sandoval return ret; 590ee056f98SOmar Sandoval } 591ee056f98SOmar Sandoval } 592d332ce09SOmar Sandoval blk_mq_debugfs_register_sched_hctx(q, hctx); 593ee056f98SOmar Sandoval } 594ee056f98SOmar Sandoval 5956917ff0bSOmar Sandoval return 0; 5966917ff0bSOmar Sandoval 5976917ff0bSOmar Sandoval err: 598c3e22192SMing Lei blk_mq_sched_free_requests(q); 59954d5329dSOmar Sandoval blk_mq_sched_tags_teardown(q); 60054d5329dSOmar Sandoval q->elevator = NULL; 6016917ff0bSOmar Sandoval return ret; 6026917ff0bSOmar Sandoval } 6036917ff0bSOmar Sandoval 604c3e22192SMing Lei /* 605c3e22192SMing Lei * called in either blk_queue_cleanup or elevator_switch, tagset 606c3e22192SMing Lei * is required for freeing requests 607c3e22192SMing Lei */ 608c3e22192SMing Lei void blk_mq_sched_free_requests(struct request_queue *q) 609c3e22192SMing Lei { 610c3e22192SMing Lei struct blk_mq_hw_ctx *hctx; 611c3e22192SMing Lei int i; 612c3e22192SMing Lei 613c3e22192SMing Lei queue_for_each_hw_ctx(q, hctx, i) { 614c3e22192SMing Lei if (hctx->sched_tags) 615c3e22192SMing Lei blk_mq_free_rqs(q->tag_set, hctx->sched_tags, i); 616c3e22192SMing Lei } 617c3e22192SMing Lei } 618c3e22192SMing Lei 61954d5329dSOmar Sandoval void blk_mq_exit_sched(struct request_queue *q, struct elevator_queue *e) 62054d5329dSOmar Sandoval { 621ee056f98SOmar Sandoval struct blk_mq_hw_ctx *hctx; 622ee056f98SOmar Sandoval unsigned int i; 623ee056f98SOmar Sandoval 624ee056f98SOmar Sandoval queue_for_each_hw_ctx(q, hctx, i) { 625d332ce09SOmar Sandoval blk_mq_debugfs_unregister_sched_hctx(hctx); 626f9cd4bfeSJens Axboe if (e->type->ops.exit_hctx && hctx->sched_data) { 627f9cd4bfeSJens Axboe e->type->ops.exit_hctx(hctx, i); 628ee056f98SOmar Sandoval hctx->sched_data = NULL; 629ee056f98SOmar Sandoval } 630ee056f98SOmar Sandoval } 631d332ce09SOmar Sandoval blk_mq_debugfs_unregister_sched(q); 632f9cd4bfeSJens Axboe if (e->type->ops.exit_sched) 633f9cd4bfeSJens Axboe e->type->ops.exit_sched(e); 63454d5329dSOmar Sandoval blk_mq_sched_tags_teardown(q); 63554d5329dSOmar Sandoval q->elevator = NULL; 63654d5329dSOmar Sandoval } 637