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 60e9ea1596SPavel Begunkov void __blk_mq_sched_restart(struct blk_mq_hw_ctx *hctx) 618e8320c9SJens Axboe { 628e8320c9SJens Axboe clear_bit(BLK_MQ_S_SCHED_RESTART, &hctx->state); 638e8320c9SJens Axboe 64d7d8535fSMing Lei /* 65d7d8535fSMing Lei * Order clearing SCHED_RESTART and list_empty_careful(&hctx->dispatch) 66d7d8535fSMing Lei * in blk_mq_run_hw_queue(). Its pair is the barrier in 67d7d8535fSMing Lei * blk_mq_dispatch_rq_list(). So dispatch code won't see SCHED_RESTART, 68d7d8535fSMing Lei * meantime new request added to hctx->dispatch is missed to check in 69d7d8535fSMing Lei * blk_mq_run_hw_queue(). 70d7d8535fSMing Lei */ 71d7d8535fSMing Lei smp_mb(); 72d7d8535fSMing Lei 7397889f9aSMing Lei blk_mq_run_hw_queue(hctx, true); 748e8320c9SJens Axboe } 758e8320c9SJens Axboe 764f0f586bSSami Tolvanen static int sched_rq_cmp(void *priv, const struct list_head *a, 774f0f586bSSami Tolvanen const struct list_head *b) 786e6fcbc2SMing Lei { 796e6fcbc2SMing Lei struct request *rqa = container_of(a, struct request, queuelist); 806e6fcbc2SMing Lei struct request *rqb = container_of(b, struct request, queuelist); 816e6fcbc2SMing Lei 826e6fcbc2SMing Lei return rqa->mq_hctx > rqb->mq_hctx; 836e6fcbc2SMing Lei } 846e6fcbc2SMing Lei 856e6fcbc2SMing Lei static bool blk_mq_dispatch_hctx_list(struct list_head *rq_list) 866e6fcbc2SMing Lei { 876e6fcbc2SMing Lei struct blk_mq_hw_ctx *hctx = 886e6fcbc2SMing Lei list_first_entry(rq_list, struct request, queuelist)->mq_hctx; 896e6fcbc2SMing Lei struct request *rq; 906e6fcbc2SMing Lei LIST_HEAD(hctx_list); 916e6fcbc2SMing Lei unsigned int count = 0; 926e6fcbc2SMing Lei 936e6fcbc2SMing Lei list_for_each_entry(rq, rq_list, queuelist) { 946e6fcbc2SMing Lei if (rq->mq_hctx != hctx) { 956e6fcbc2SMing Lei list_cut_before(&hctx_list, rq_list, &rq->queuelist); 966e6fcbc2SMing Lei goto dispatch; 976e6fcbc2SMing Lei } 986e6fcbc2SMing Lei count++; 996e6fcbc2SMing Lei } 1006e6fcbc2SMing Lei list_splice_tail_init(rq_list, &hctx_list); 1016e6fcbc2SMing Lei 1026e6fcbc2SMing Lei dispatch: 103106e71c5SBaolin Wang return blk_mq_dispatch_rq_list(hctx, &hctx_list, count); 1046e6fcbc2SMing Lei } 1056e6fcbc2SMing Lei 106a0823421SDouglas Anderson #define BLK_MQ_BUDGET_DELAY 3 /* ms units */ 107a0823421SDouglas Anderson 1081f460b63SMing Lei /* 1091f460b63SMing Lei * Only SCSI implements .get_budget and .put_budget, and SCSI restarts 1101f460b63SMing Lei * its queue by itself in its completion handler, so we don't need to 1111f460b63SMing Lei * restart queue if .get_budget() returns BLK_STS_NO_RESOURCE. 11228d65729SSalman Qazi * 11328d65729SSalman Qazi * Returns -EAGAIN if hctx->dispatch was found non-empty and run_work has to 11428d65729SSalman Qazi * be run again. This is necessary to avoid starving flushes. 1151f460b63SMing Lei */ 1166e6fcbc2SMing Lei static int __blk_mq_do_dispatch_sched(struct blk_mq_hw_ctx *hctx) 117caf8eb0dSMing Lei { 118caf8eb0dSMing Lei struct request_queue *q = hctx->queue; 119caf8eb0dSMing Lei struct elevator_queue *e = q->elevator; 1206e6fcbc2SMing Lei bool multi_hctxs = false, run_queue = false; 1216e6fcbc2SMing Lei bool dispatched = false, busy = false; 1226e6fcbc2SMing Lei unsigned int max_dispatch; 123caf8eb0dSMing Lei LIST_HEAD(rq_list); 1246e6fcbc2SMing Lei int count = 0; 1256e6fcbc2SMing Lei 1266e6fcbc2SMing Lei if (hctx->dispatch_busy) 1276e6fcbc2SMing Lei max_dispatch = 1; 1286e6fcbc2SMing Lei else 1296e6fcbc2SMing Lei max_dispatch = hctx->queue->nr_requests; 130caf8eb0dSMing Lei 131445874e8SMing Lei do { 1326e6fcbc2SMing Lei struct request *rq; 1332a5a24aaSMing Lei int budget_token; 1346e6fcbc2SMing Lei 135f9cd4bfeSJens Axboe if (e->type->ops.has_work && !e->type->ops.has_work(hctx)) 136caf8eb0dSMing Lei break; 137de148297SMing Lei 13828d65729SSalman Qazi if (!list_empty_careful(&hctx->dispatch)) { 1396e6fcbc2SMing Lei busy = true; 14028d65729SSalman Qazi break; 14128d65729SSalman Qazi } 14228d65729SSalman Qazi 1432a5a24aaSMing Lei budget_token = blk_mq_get_dispatch_budget(q); 1442a5a24aaSMing Lei if (budget_token < 0) 1451f460b63SMing Lei break; 146de148297SMing Lei 147f9cd4bfeSJens Axboe rq = e->type->ops.dispatch_request(hctx); 148de148297SMing Lei if (!rq) { 1492a5a24aaSMing Lei blk_mq_put_dispatch_budget(q, budget_token); 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 1612a5a24aaSMing Lei blk_mq_set_rq_budget_token(rq, budget_token); 1622a5a24aaSMing Lei 163de148297SMing Lei /* 164de148297SMing Lei * Now this rq owns the budget which has to be released 165de148297SMing Lei * if this rq won't be queued to driver via .queue_rq() 166de148297SMing Lei * in blk_mq_dispatch_rq_list(). 167de148297SMing Lei */ 1686e6fcbc2SMing Lei list_add_tail(&rq->queuelist, &rq_list); 16961347154SJan Kara count++; 1706e6fcbc2SMing Lei if (rq->mq_hctx != hctx) 1716e6fcbc2SMing Lei multi_hctxs = true; 17261347154SJan Kara 17361347154SJan Kara /* 17461347154SJan Kara * If we cannot get tag for the request, stop dequeueing 17561347154SJan Kara * requests from the IO scheduler. We are unlikely to be able 17661347154SJan Kara * to submit them anyway and it creates false impression for 17761347154SJan Kara * scheduling heuristics that the device can take more IO. 17861347154SJan Kara */ 17961347154SJan Kara if (!blk_mq_get_driver_tag(rq)) 18061347154SJan Kara break; 18161347154SJan Kara } while (count < max_dispatch); 1826e6fcbc2SMing Lei 1836e6fcbc2SMing Lei if (!count) { 1846e6fcbc2SMing Lei if (run_queue) 1856e6fcbc2SMing Lei blk_mq_delay_run_hw_queues(q, BLK_MQ_BUDGET_DELAY); 1866e6fcbc2SMing Lei } else if (multi_hctxs) { 1876e6fcbc2SMing Lei /* 1886e6fcbc2SMing Lei * Requests from different hctx may be dequeued from some 1896e6fcbc2SMing Lei * schedulers, such as bfq and deadline. 1906e6fcbc2SMing Lei * 1916e6fcbc2SMing Lei * Sort the requests in the list according to their hctx, 1926e6fcbc2SMing Lei * dispatch batching requests from same hctx at a time. 1936e6fcbc2SMing Lei */ 1946e6fcbc2SMing Lei list_sort(NULL, &rq_list, sched_rq_cmp); 1956e6fcbc2SMing Lei do { 1966e6fcbc2SMing Lei dispatched |= blk_mq_dispatch_hctx_list(&rq_list); 1976e6fcbc2SMing Lei } while (!list_empty(&rq_list)); 1986e6fcbc2SMing Lei } else { 1996e6fcbc2SMing Lei dispatched = blk_mq_dispatch_rq_list(hctx, &rq_list, count); 2006e6fcbc2SMing Lei } 2016e6fcbc2SMing Lei 2026e6fcbc2SMing Lei if (busy) 2036e6fcbc2SMing Lei return -EAGAIN; 2046e6fcbc2SMing Lei return !!dispatched; 2056e6fcbc2SMing Lei } 2066e6fcbc2SMing Lei 2076e6fcbc2SMing Lei static int blk_mq_do_dispatch_sched(struct blk_mq_hw_ctx *hctx) 2086e6fcbc2SMing Lei { 2096e6fcbc2SMing Lei int ret; 2106e6fcbc2SMing Lei 2116e6fcbc2SMing Lei do { 2126e6fcbc2SMing Lei ret = __blk_mq_do_dispatch_sched(hctx); 2136e6fcbc2SMing Lei } while (ret == 1); 21428d65729SSalman Qazi 21528d65729SSalman Qazi return ret; 216de148297SMing Lei } 217de148297SMing Lei 218b347689fSMing Lei static struct blk_mq_ctx *blk_mq_next_ctx(struct blk_mq_hw_ctx *hctx, 219b347689fSMing Lei struct blk_mq_ctx *ctx) 220b347689fSMing Lei { 221f31967f0SJens Axboe unsigned short idx = ctx->index_hw[hctx->type]; 222b347689fSMing Lei 223b347689fSMing Lei if (++idx == hctx->nr_ctx) 224b347689fSMing Lei idx = 0; 225b347689fSMing Lei 226b347689fSMing Lei return hctx->ctxs[idx]; 227b347689fSMing Lei } 228b347689fSMing Lei 2291f460b63SMing Lei /* 2301f460b63SMing Lei * Only SCSI implements .get_budget and .put_budget, and SCSI restarts 2311f460b63SMing Lei * its queue by itself in its completion handler, so we don't need to 2321f460b63SMing Lei * restart queue if .get_budget() returns BLK_STS_NO_RESOURCE. 23328d65729SSalman Qazi * 23428d65729SSalman Qazi * Returns -EAGAIN if hctx->dispatch was found non-empty and run_work has to 235c4aecaa2SRandy Dunlap * be run again. This is necessary to avoid starving flushes. 2361f460b63SMing Lei */ 23728d65729SSalman Qazi static int blk_mq_do_dispatch_ctx(struct blk_mq_hw_ctx *hctx) 238b347689fSMing Lei { 239b347689fSMing Lei struct request_queue *q = hctx->queue; 240b347689fSMing Lei LIST_HEAD(rq_list); 241b347689fSMing Lei struct blk_mq_ctx *ctx = READ_ONCE(hctx->dispatch_from); 24228d65729SSalman Qazi int ret = 0; 243b347689fSMing Lei struct request *rq; 244b347689fSMing Lei 245445874e8SMing Lei do { 2462a5a24aaSMing Lei int budget_token; 2472a5a24aaSMing Lei 24828d65729SSalman Qazi if (!list_empty_careful(&hctx->dispatch)) { 24928d65729SSalman Qazi ret = -EAGAIN; 25028d65729SSalman Qazi break; 25128d65729SSalman Qazi } 25228d65729SSalman Qazi 253b347689fSMing Lei if (!sbitmap_any_bit_set(&hctx->ctx_map)) 254b347689fSMing Lei break; 255b347689fSMing Lei 2562a5a24aaSMing Lei budget_token = blk_mq_get_dispatch_budget(q); 2572a5a24aaSMing Lei if (budget_token < 0) 2581f460b63SMing Lei break; 259b347689fSMing Lei 260b347689fSMing Lei rq = blk_mq_dequeue_from_ctx(hctx, ctx); 261b347689fSMing Lei if (!rq) { 2622a5a24aaSMing Lei blk_mq_put_dispatch_budget(q, budget_token); 263a0823421SDouglas Anderson /* 264a0823421SDouglas Anderson * We're releasing without dispatching. Holding the 265a0823421SDouglas Anderson * budget could have blocked any "hctx"s with the 266a0823421SDouglas Anderson * same queue and if we didn't dispatch then there's 267a0823421SDouglas Anderson * no guarantee anyone will kick the queue. Kick it 268a0823421SDouglas Anderson * ourselves. 269a0823421SDouglas Anderson */ 270a0823421SDouglas Anderson blk_mq_delay_run_hw_queues(q, BLK_MQ_BUDGET_DELAY); 271b347689fSMing Lei break; 272b347689fSMing Lei } 273b347689fSMing Lei 2742a5a24aaSMing Lei blk_mq_set_rq_budget_token(rq, budget_token); 2752a5a24aaSMing Lei 276b347689fSMing Lei /* 277b347689fSMing Lei * Now this rq owns the budget which has to be released 278b347689fSMing Lei * if this rq won't be queued to driver via .queue_rq() 279b347689fSMing Lei * in blk_mq_dispatch_rq_list(). 280b347689fSMing Lei */ 281b347689fSMing Lei list_add(&rq->queuelist, &rq_list); 282b347689fSMing Lei 283b347689fSMing Lei /* round robin for fair dispatch */ 284b347689fSMing Lei ctx = blk_mq_next_ctx(hctx, rq->mq_ctx); 285b347689fSMing Lei 2861fd40b5eSMing Lei } while (blk_mq_dispatch_rq_list(rq->mq_hctx, &rq_list, 1)); 287b347689fSMing Lei 288b347689fSMing Lei WRITE_ONCE(hctx->dispatch_from, ctx); 28928d65729SSalman Qazi return ret; 290b347689fSMing Lei } 291b347689fSMing Lei 292e1b586f2SZheng Bin static int __blk_mq_sched_dispatch_requests(struct blk_mq_hw_ctx *hctx) 293bd166ef1SJens Axboe { 29481380ca1SOmar Sandoval struct request_queue *q = hctx->queue; 295e42cfb1dSDamien Le Moal const bool has_sched = q->elevator; 29628d65729SSalman Qazi int ret = 0; 297bd166ef1SJens Axboe LIST_HEAD(rq_list); 298bd166ef1SJens Axboe 299bd166ef1SJens Axboe /* 300bd166ef1SJens Axboe * If we have previous entries on our dispatch list, grab them first for 301bd166ef1SJens Axboe * more fair dispatch. 302bd166ef1SJens Axboe */ 303bd166ef1SJens Axboe if (!list_empty_careful(&hctx->dispatch)) { 304bd166ef1SJens Axboe spin_lock(&hctx->lock); 305bd166ef1SJens Axboe if (!list_empty(&hctx->dispatch)) 306bd166ef1SJens Axboe list_splice_init(&hctx->dispatch, &rq_list); 307bd166ef1SJens Axboe spin_unlock(&hctx->lock); 308bd166ef1SJens Axboe } 309bd166ef1SJens Axboe 310bd166ef1SJens Axboe /* 311bd166ef1SJens Axboe * Only ask the scheduler for requests, if we didn't have residual 312bd166ef1SJens Axboe * requests from the dispatch list. This is to avoid the case where 313bd166ef1SJens Axboe * we only ever dispatch a fraction of the requests available because 314bd166ef1SJens Axboe * of low device queue depth. Once we pull requests out of the IO 315bd166ef1SJens Axboe * scheduler, we can no longer merge or sort them. So it's best to 316bd166ef1SJens Axboe * leave them there for as long as we can. Mark the hw queue as 317bd166ef1SJens Axboe * needing a restart in that case. 318caf8eb0dSMing Lei * 3195e3d02bbSMing Lei * We want to dispatch from the scheduler if there was nothing 3205e3d02bbSMing Lei * on the dispatch list or we were able to dispatch from the 3215e3d02bbSMing Lei * dispatch list. 32264765a75SJens Axboe */ 323caf8eb0dSMing Lei if (!list_empty(&rq_list)) { 324caf8eb0dSMing Lei blk_mq_sched_mark_restart_hctx(hctx); 3251fd40b5eSMing Lei if (blk_mq_dispatch_rq_list(hctx, &rq_list, 0)) { 326e42cfb1dSDamien Le Moal if (has_sched) 32728d65729SSalman Qazi ret = blk_mq_do_dispatch_sched(hctx); 328b347689fSMing Lei else 32928d65729SSalman Qazi ret = blk_mq_do_dispatch_ctx(hctx); 330b347689fSMing Lei } 331e42cfb1dSDamien Le Moal } else if (has_sched) { 33228d65729SSalman Qazi ret = blk_mq_do_dispatch_sched(hctx); 3336e768717SMing Lei } else if (hctx->dispatch_busy) { 3346e768717SMing Lei /* dequeue request one by one from sw queue if queue is busy */ 33528d65729SSalman Qazi ret = blk_mq_do_dispatch_ctx(hctx); 336caf8eb0dSMing Lei } else { 337caf8eb0dSMing Lei blk_mq_flush_busy_ctxs(hctx, &rq_list); 3381fd40b5eSMing Lei blk_mq_dispatch_rq_list(hctx, &rq_list, 0); 339c13660a0SJens Axboe } 34028d65729SSalman Qazi 34128d65729SSalman Qazi return ret; 34228d65729SSalman Qazi } 34328d65729SSalman Qazi 34428d65729SSalman Qazi void blk_mq_sched_dispatch_requests(struct blk_mq_hw_ctx *hctx) 34528d65729SSalman Qazi { 34628d65729SSalman Qazi struct request_queue *q = hctx->queue; 34728d65729SSalman Qazi 34828d65729SSalman Qazi /* RCU or SRCU read lock is needed before checking quiesced flag */ 34928d65729SSalman Qazi if (unlikely(blk_mq_hctx_stopped(hctx) || blk_queue_quiesced(q))) 35028d65729SSalman Qazi return; 35128d65729SSalman Qazi 35228d65729SSalman Qazi hctx->run++; 35328d65729SSalman Qazi 35428d65729SSalman Qazi /* 35528d65729SSalman Qazi * A return of -EAGAIN is an indication that hctx->dispatch is not 35628d65729SSalman Qazi * empty and we must run again in order to avoid starving flushes. 35728d65729SSalman Qazi */ 35828d65729SSalman Qazi if (__blk_mq_sched_dispatch_requests(hctx) == -EAGAIN) { 35928d65729SSalman Qazi if (__blk_mq_sched_dispatch_requests(hctx) == -EAGAIN) 36028d65729SSalman Qazi blk_mq_run_hw_queue(hctx, true); 36128d65729SSalman Qazi } 362bd166ef1SJens Axboe } 363bd166ef1SJens Axboe 364179ae84fSPavel Begunkov bool blk_mq_sched_bio_merge(struct request_queue *q, struct bio *bio, 36514ccb66bSChristoph Hellwig unsigned int nr_segs) 366bd166ef1SJens Axboe { 367bd166ef1SJens Axboe struct elevator_queue *e = q->elevator; 368efed9a33SOmar Sandoval struct blk_mq_ctx *ctx; 369efed9a33SOmar Sandoval struct blk_mq_hw_ctx *hctx; 3709bddeb2aSMing Lei bool ret = false; 371c16d6b5aSMing Lei enum hctx_type type; 372bd166ef1SJens Axboe 373*900e0807SJens Axboe if (bio_queue_enter(bio)) 374*900e0807SJens Axboe return false; 375*900e0807SJens Axboe 376*900e0807SJens Axboe if (e && e->type->ops.bio_merge) { 377*900e0807SJens Axboe ret = e->type->ops.bio_merge(q, bio, nr_segs); 378*900e0807SJens Axboe goto out_put; 379*900e0807SJens Axboe } 380bd166ef1SJens Axboe 381efed9a33SOmar Sandoval ctx = blk_mq_get_ctx(q); 382efed9a33SOmar Sandoval hctx = blk_mq_map_queue(q, bio->bi_opf, ctx); 383c16d6b5aSMing Lei type = hctx->type; 384cdfcef9eSBaolin Wang if (!(hctx->flags & BLK_MQ_F_SHOULD_MERGE) || 385cdfcef9eSBaolin Wang list_empty_careful(&ctx->rq_lists[type])) 386*900e0807SJens Axboe goto out_put; 387cdfcef9eSBaolin Wang 3889bddeb2aSMing Lei /* default per sw-queue merge */ 3899bddeb2aSMing Lei spin_lock(&ctx->lock); 390cdfcef9eSBaolin Wang /* 391cdfcef9eSBaolin Wang * Reverse check our software queue for entries that we could 392cdfcef9eSBaolin Wang * potentially merge with. Currently includes a hand-wavy stop 393cdfcef9eSBaolin Wang * count of 8, to not spend too much time checking for merges. 394cdfcef9eSBaolin Wang */ 3959a14d6ceSJens Axboe if (blk_bio_list_merge(q, &ctx->rq_lists[type], bio, nr_segs)) 396cdfcef9eSBaolin Wang ret = true; 3979bddeb2aSMing Lei 398cdfcef9eSBaolin Wang spin_unlock(&ctx->lock); 399*900e0807SJens Axboe out_put: 400*900e0807SJens Axboe blk_queue_exit(q); 4019bddeb2aSMing Lei return ret; 402bd166ef1SJens Axboe } 403bd166ef1SJens Axboe 404fd2ef39cSJan Kara bool blk_mq_sched_try_insert_merge(struct request_queue *q, struct request *rq, 405fd2ef39cSJan Kara struct list_head *free) 406bd166ef1SJens Axboe { 407fd2ef39cSJan Kara return rq_mergeable(rq) && elv_attempt_insert_merge(q, rq, free); 408bd166ef1SJens Axboe } 409bd166ef1SJens Axboe EXPORT_SYMBOL_GPL(blk_mq_sched_try_insert_merge); 410bd166ef1SJens Axboe 4110cacba6cSOmar Sandoval static bool blk_mq_sched_bypass_insert(struct blk_mq_hw_ctx *hctx, 4120cacba6cSOmar Sandoval struct request *rq) 413bd166ef1SJens Axboe { 41401e99aecSMing Lei /* 41501e99aecSMing Lei * dispatch flush and passthrough rq directly 41601e99aecSMing Lei * 41701e99aecSMing Lei * passthrough request has to be added to hctx->dispatch directly. 41801e99aecSMing Lei * For some reason, device may be in one situation which can't 41901e99aecSMing Lei * handle FS request, so STS_RESOURCE is always returned and the 42001e99aecSMing Lei * FS request will be added to hctx->dispatch. However passthrough 42101e99aecSMing Lei * request may be required at that time for fixing the problem. If 42201e99aecSMing Lei * passthrough request is added to scheduler queue, there isn't any 42301e99aecSMing Lei * chance to dispatch it given we prioritize requests in hctx->dispatch. 42401e99aecSMing Lei */ 42501e99aecSMing Lei if ((rq->rq_flags & RQF_FLUSH_SEQ) || blk_rq_is_passthrough(rq)) 426bd166ef1SJens Axboe return true; 427bd166ef1SJens Axboe 428a6a252e6SMing Lei return false; 429a6a252e6SMing Lei } 430a6a252e6SMing Lei 431bd6737f1SJens Axboe void blk_mq_sched_insert_request(struct request *rq, bool at_head, 4329e97d295SMike Snitzer bool run_queue, bool async) 433bd6737f1SJens Axboe { 434bd6737f1SJens Axboe struct request_queue *q = rq->q; 435bd6737f1SJens Axboe struct elevator_queue *e = q->elevator; 436bd6737f1SJens Axboe struct blk_mq_ctx *ctx = rq->mq_ctx; 437ea4f995eSJens Axboe struct blk_mq_hw_ctx *hctx = rq->mq_hctx; 438bd6737f1SJens Axboe 439e44a6a23SXianting Tian WARN_ON(e && (rq->tag != BLK_MQ_NO_TAG)); 440923218f6SMing Lei 4415218e12eSJean Delvare if (blk_mq_sched_bypass_insert(hctx, rq)) { 442cc3200eaSMing Lei /* 443cc3200eaSMing Lei * Firstly normal IO request is inserted to scheduler queue or 444cc3200eaSMing Lei * sw queue, meantime we add flush request to dispatch queue( 445cc3200eaSMing Lei * hctx->dispatch) directly and there is at most one in-flight 446cc3200eaSMing Lei * flush request for each hw queue, so it doesn't matter to add 447cc3200eaSMing Lei * flush request to tail or front of the dispatch queue. 448cc3200eaSMing Lei * 449cc3200eaSMing Lei * Secondly in case of NCQ, flush request belongs to non-NCQ 450cc3200eaSMing Lei * command, and queueing it will fail when there is any 451cc3200eaSMing Lei * in-flight normal IO request(NCQ command). When adding flush 452cc3200eaSMing Lei * rq to the front of hctx->dispatch, it is easier to introduce 453cc3200eaSMing Lei * extra time to flush rq's latency because of S_SCHED_RESTART 454cc3200eaSMing Lei * compared with adding to the tail of dispatch queue, then 455cc3200eaSMing Lei * chance of flush merge is increased, and less flush requests 456cc3200eaSMing Lei * will be issued to controller. It is observed that ~10% time 457cc3200eaSMing Lei * is saved in blktests block/004 on disk attached to AHCI/NCQ 458cc3200eaSMing Lei * drive when adding flush rq to the front of hctx->dispatch. 459cc3200eaSMing Lei * 460cc3200eaSMing Lei * Simply queue flush rq to the front of hctx->dispatch so that 461cc3200eaSMing Lei * intensive flush workloads can benefit in case of NCQ HW. 462cc3200eaSMing Lei */ 463cc3200eaSMing Lei at_head = (rq->rq_flags & RQF_FLUSH_SEQ) ? true : at_head; 46401e99aecSMing Lei blk_mq_request_bypass_insert(rq, at_head, false); 4650cacba6cSOmar Sandoval goto run; 46601e99aecSMing Lei } 4670cacba6cSOmar Sandoval 468e42cfb1dSDamien Le Moal if (e) { 469bd6737f1SJens Axboe LIST_HEAD(list); 470bd6737f1SJens Axboe 471bd6737f1SJens Axboe list_add(&rq->queuelist, &list); 472f9cd4bfeSJens Axboe e->type->ops.insert_requests(hctx, &list, at_head); 473bd6737f1SJens Axboe } else { 474bd6737f1SJens Axboe spin_lock(&ctx->lock); 475bd6737f1SJens Axboe __blk_mq_insert_request(hctx, rq, at_head); 476bd6737f1SJens Axboe spin_unlock(&ctx->lock); 477bd6737f1SJens Axboe } 478bd6737f1SJens Axboe 4790cacba6cSOmar Sandoval run: 480bd6737f1SJens Axboe if (run_queue) 481bd6737f1SJens Axboe blk_mq_run_hw_queue(hctx, async); 482bd6737f1SJens Axboe } 483bd6737f1SJens Axboe 48467cae4c9SJens Axboe void blk_mq_sched_insert_requests(struct blk_mq_hw_ctx *hctx, 485bd6737f1SJens Axboe struct blk_mq_ctx *ctx, 486bd6737f1SJens Axboe struct list_head *list, bool run_queue_async) 487bd6737f1SJens Axboe { 488f9afca4dSJens Axboe struct elevator_queue *e; 489e87eb301SMing Lei struct request_queue *q = hctx->queue; 490e87eb301SMing Lei 491e87eb301SMing Lei /* 492e87eb301SMing Lei * blk_mq_sched_insert_requests() is called from flush plug 493e87eb301SMing Lei * context only, and hold one usage counter to prevent queue 494e87eb301SMing Lei * from being released. 495e87eb301SMing Lei */ 496e87eb301SMing Lei percpu_ref_get(&q->q_usage_counter); 497f9afca4dSJens Axboe 498f9afca4dSJens Axboe e = hctx->queue->elevator; 499e42cfb1dSDamien Le Moal if (e) { 500f9cd4bfeSJens Axboe e->type->ops.insert_requests(hctx, list, false); 501e42cfb1dSDamien Le Moal } else { 5026ce3dd6eSMing Lei /* 5036ce3dd6eSMing Lei * try to issue requests directly if the hw queue isn't 5046ce3dd6eSMing Lei * busy in case of 'none' scheduler, and this way may save 5056ce3dd6eSMing Lei * us one extra enqueue & dequeue to sw queue. 5066ce3dd6eSMing Lei */ 507ef1661baSJean Sacren if (!hctx->dispatch_busy && !run_queue_async) { 5086ce3dd6eSMing Lei blk_mq_try_issue_list_directly(hctx, list); 509fd9c40f6SBart Van Assche if (list_empty(list)) 510e87eb301SMing Lei goto out; 511fd9c40f6SBart Van Assche } 512bd6737f1SJens Axboe blk_mq_insert_requests(hctx, ctx, list); 5136ce3dd6eSMing Lei } 514bd6737f1SJens Axboe 515bd6737f1SJens Axboe blk_mq_run_hw_queue(hctx, run_queue_async); 516e87eb301SMing Lei out: 517e87eb301SMing Lei percpu_ref_put(&q->q_usage_counter); 518bd6737f1SJens Axboe } 519bd6737f1SJens Axboe 520d99a6bb3SJohn Garry static int blk_mq_sched_alloc_map_and_rqs(struct request_queue *q, 5216917ff0bSOmar Sandoval struct blk_mq_hw_ctx *hctx, 5226917ff0bSOmar Sandoval unsigned int hctx_idx) 523bd166ef1SJens Axboe { 524079a2e3eSJohn Garry if (blk_mq_is_shared_tags(q->tag_set->flags)) { 525079a2e3eSJohn Garry hctx->sched_tags = q->sched_shared_tags; 526e155b0c2SJohn Garry return 0; 527e155b0c2SJohn Garry } 528e155b0c2SJohn Garry 52963064be1SJohn Garry hctx->sched_tags = blk_mq_alloc_map_and_rqs(q->tag_set, hctx_idx, 53063064be1SJohn Garry q->nr_requests); 531bd166ef1SJens Axboe 532bd166ef1SJens Axboe if (!hctx->sched_tags) 5336917ff0bSOmar Sandoval return -ENOMEM; 53463064be1SJohn Garry return 0; 535bd166ef1SJens Axboe } 536bd166ef1SJens Axboe 537079a2e3eSJohn Garry static void blk_mq_exit_sched_shared_tags(struct request_queue *queue) 538e155b0c2SJohn Garry { 539079a2e3eSJohn Garry blk_mq_free_rq_map(queue->sched_shared_tags); 540079a2e3eSJohn Garry queue->sched_shared_tags = NULL; 541e155b0c2SJohn Garry } 542e155b0c2SJohn Garry 543c3e22192SMing Lei /* called in queue's release handler, tagset has gone away */ 544e155b0c2SJohn Garry static void blk_mq_sched_tags_teardown(struct request_queue *q, unsigned int flags) 545bd166ef1SJens Axboe { 546bd166ef1SJens Axboe struct blk_mq_hw_ctx *hctx; 547bd166ef1SJens Axboe int i; 548bd166ef1SJens Axboe 549c3e22192SMing Lei queue_for_each_hw_ctx(q, hctx, i) { 550c3e22192SMing Lei if (hctx->sched_tags) { 5518bdf7b3fSJohn Garry if (!blk_mq_is_shared_tags(flags)) 552e155b0c2SJohn Garry blk_mq_free_rq_map(hctx->sched_tags); 553c3e22192SMing Lei hctx->sched_tags = NULL; 554c3e22192SMing Lei } 555c3e22192SMing Lei } 556e155b0c2SJohn Garry 557079a2e3eSJohn Garry if (blk_mq_is_shared_tags(flags)) 558079a2e3eSJohn Garry blk_mq_exit_sched_shared_tags(q); 559bd166ef1SJens Axboe } 560d3484991SJens Axboe 561079a2e3eSJohn Garry static int blk_mq_init_sched_shared_tags(struct request_queue *queue) 562d97e594cSJohn Garry { 563d97e594cSJohn Garry struct blk_mq_tag_set *set = queue->tag_set; 564d97e594cSJohn Garry 565d97e594cSJohn Garry /* 566d97e594cSJohn Garry * Set initial depth at max so that we don't need to reallocate for 567d97e594cSJohn Garry * updating nr_requests. 568d97e594cSJohn Garry */ 569079a2e3eSJohn Garry queue->sched_shared_tags = blk_mq_alloc_map_and_rqs(set, 570e155b0c2SJohn Garry BLK_MQ_NO_HCTX_IDX, 571e155b0c2SJohn Garry MAX_SCHED_RQ); 572079a2e3eSJohn Garry if (!queue->sched_shared_tags) 573e155b0c2SJohn Garry return -ENOMEM; 574d97e594cSJohn Garry 575079a2e3eSJohn Garry blk_mq_tag_update_sched_shared_tags(queue); 576d97e594cSJohn Garry 577d97e594cSJohn Garry return 0; 578d97e594cSJohn Garry } 579d97e594cSJohn Garry 5806917ff0bSOmar Sandoval int blk_mq_init_sched(struct request_queue *q, struct elevator_type *e) 5816917ff0bSOmar Sandoval { 582e155b0c2SJohn Garry unsigned int i, flags = q->tag_set->flags; 5836917ff0bSOmar Sandoval struct blk_mq_hw_ctx *hctx; 584ee056f98SOmar Sandoval struct elevator_queue *eq; 5856917ff0bSOmar Sandoval int ret; 5866917ff0bSOmar Sandoval 5876917ff0bSOmar Sandoval if (!e) { 5886917ff0bSOmar Sandoval q->elevator = NULL; 58932a50fabSMing Lei q->nr_requests = q->tag_set->queue_depth; 5906917ff0bSOmar Sandoval return 0; 5916917ff0bSOmar Sandoval } 5926917ff0bSOmar Sandoval 5936917ff0bSOmar Sandoval /* 59432825c45SMing Lei * Default to double of smaller one between hw queue_depth and 128, 59532825c45SMing Lei * since we don't split into sync/async like the old code did. 59632825c45SMing Lei * Additionally, this is a per-hw queue depth. 5976917ff0bSOmar Sandoval */ 59832825c45SMing Lei q->nr_requests = 2 * min_t(unsigned int, q->tag_set->queue_depth, 599d2a27964SJohn Garry BLKDEV_DEFAULT_RQ); 6006917ff0bSOmar Sandoval 601079a2e3eSJohn Garry if (blk_mq_is_shared_tags(flags)) { 602079a2e3eSJohn Garry ret = blk_mq_init_sched_shared_tags(q); 603e155b0c2SJohn Garry if (ret) 604e155b0c2SJohn Garry return ret; 605e155b0c2SJohn Garry } 606e155b0c2SJohn Garry 6076917ff0bSOmar Sandoval queue_for_each_hw_ctx(q, hctx, i) { 608d99a6bb3SJohn Garry ret = blk_mq_sched_alloc_map_and_rqs(q, hctx, i); 6096917ff0bSOmar Sandoval if (ret) 610d99a6bb3SJohn Garry goto err_free_map_and_rqs; 611d97e594cSJohn Garry } 612d97e594cSJohn Garry 613f9cd4bfeSJens Axboe ret = e->ops.init_sched(q, e); 6146917ff0bSOmar Sandoval if (ret) 615e155b0c2SJohn Garry goto err_free_map_and_rqs; 6166917ff0bSOmar Sandoval 617d332ce09SOmar Sandoval blk_mq_debugfs_register_sched(q); 618d332ce09SOmar Sandoval 619ee056f98SOmar Sandoval queue_for_each_hw_ctx(q, hctx, i) { 620f9cd4bfeSJens Axboe if (e->ops.init_hctx) { 621f9cd4bfeSJens Axboe ret = e->ops.init_hctx(hctx, i); 622ee056f98SOmar Sandoval if (ret) { 623ee056f98SOmar Sandoval eq = q->elevator; 6241820f4f0SJohn Garry blk_mq_sched_free_rqs(q); 625ee056f98SOmar Sandoval blk_mq_exit_sched(q, eq); 626ee056f98SOmar Sandoval kobject_put(&eq->kobj); 627ee056f98SOmar Sandoval return ret; 628ee056f98SOmar Sandoval } 629ee056f98SOmar Sandoval } 630d332ce09SOmar Sandoval blk_mq_debugfs_register_sched_hctx(q, hctx); 631ee056f98SOmar Sandoval } 632ee056f98SOmar Sandoval 6336917ff0bSOmar Sandoval return 0; 6346917ff0bSOmar Sandoval 635d99a6bb3SJohn Garry err_free_map_and_rqs: 6361820f4f0SJohn Garry blk_mq_sched_free_rqs(q); 637e155b0c2SJohn Garry blk_mq_sched_tags_teardown(q, flags); 638e155b0c2SJohn Garry 63954d5329dSOmar Sandoval q->elevator = NULL; 6406917ff0bSOmar Sandoval return ret; 6416917ff0bSOmar Sandoval } 6426917ff0bSOmar Sandoval 643c3e22192SMing Lei /* 644c3e22192SMing Lei * called in either blk_queue_cleanup or elevator_switch, tagset 645c3e22192SMing Lei * is required for freeing requests 646c3e22192SMing Lei */ 6471820f4f0SJohn Garry void blk_mq_sched_free_rqs(struct request_queue *q) 648c3e22192SMing Lei { 649c3e22192SMing Lei struct blk_mq_hw_ctx *hctx; 650c3e22192SMing Lei int i; 651c3e22192SMing Lei 652079a2e3eSJohn Garry if (blk_mq_is_shared_tags(q->tag_set->flags)) { 653079a2e3eSJohn Garry blk_mq_free_rqs(q->tag_set, q->sched_shared_tags, 654e155b0c2SJohn Garry BLK_MQ_NO_HCTX_IDX); 655e155b0c2SJohn Garry } else { 656c3e22192SMing Lei queue_for_each_hw_ctx(q, hctx, i) { 657c3e22192SMing Lei if (hctx->sched_tags) 658e155b0c2SJohn Garry blk_mq_free_rqs(q->tag_set, 659e155b0c2SJohn Garry hctx->sched_tags, i); 660e155b0c2SJohn Garry } 661c3e22192SMing Lei } 662c3e22192SMing Lei } 663c3e22192SMing Lei 66454d5329dSOmar Sandoval void blk_mq_exit_sched(struct request_queue *q, struct elevator_queue *e) 66554d5329dSOmar Sandoval { 666ee056f98SOmar Sandoval struct blk_mq_hw_ctx *hctx; 667ee056f98SOmar Sandoval unsigned int i; 668f0c1c4d2SMing Lei unsigned int flags = 0; 669ee056f98SOmar Sandoval 670ee056f98SOmar Sandoval queue_for_each_hw_ctx(q, hctx, i) { 671d332ce09SOmar Sandoval blk_mq_debugfs_unregister_sched_hctx(hctx); 672f9cd4bfeSJens Axboe if (e->type->ops.exit_hctx && hctx->sched_data) { 673f9cd4bfeSJens Axboe e->type->ops.exit_hctx(hctx, i); 674ee056f98SOmar Sandoval hctx->sched_data = NULL; 675ee056f98SOmar Sandoval } 676f0c1c4d2SMing Lei flags = hctx->flags; 677ee056f98SOmar Sandoval } 678d332ce09SOmar Sandoval blk_mq_debugfs_unregister_sched(q); 679f9cd4bfeSJens Axboe if (e->type->ops.exit_sched) 680f9cd4bfeSJens Axboe e->type->ops.exit_sched(e); 681e155b0c2SJohn Garry blk_mq_sched_tags_teardown(q, flags); 68254d5329dSOmar Sandoval q->elevator = NULL; 68354d5329dSOmar Sandoval } 684