1 #include <linux/kernel.h>
2 #include <linux/module.h>
3 #include <linux/backing-dev.h>
5 #include <linux/blkdev.h>
7 #include <linux/init.h>
8 #include <linux/slab.h>
9 #include <linux/workqueue.h>
10 #include <linux/smp.h>
11 #include <linux/llist.h>
12 #include <linux/list_sort.h>
13 #include <linux/cpu.h>
14 #include <linux/cache.h>
15 #include <linux/sched/sysctl.h>
16 #include <linux/delay.h>
18 #include <trace/events/block.h>
20 #include <linux/blk-mq.h>
23 #include "blk-mq-tag.h"
25 static DEFINE_MUTEX(all_q_mutex);
26 static LIST_HEAD(all_q_list);
28 static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx);
30 static struct blk_mq_ctx *__blk_mq_get_ctx(struct request_queue *q,
33 return per_cpu_ptr(q->queue_ctx, cpu);
37 * This assumes per-cpu software queueing queues. They could be per-node
38 * as well, for instance. For now this is hardcoded as-is. Note that we don't
39 * care about preemption, since we know the ctx's are persistent. This does
40 * mean that we can't rely on ctx always matching the currently running CPU.
42 static struct blk_mq_ctx *blk_mq_get_ctx(struct request_queue *q)
44 return __blk_mq_get_ctx(q, get_cpu());
47 static void blk_mq_put_ctx(struct blk_mq_ctx *ctx)
53 * Check if any of the ctx's have pending work in this hardware queue
55 static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx)
59 for (i = 0; i < hctx->nr_ctx_map; i++)
67 * Mark this ctx as having pending work in this hardware queue
69 static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx,
70 struct blk_mq_ctx *ctx)
72 if (!test_bit(ctx->index_hw, hctx->ctx_map))
73 set_bit(ctx->index_hw, hctx->ctx_map);
76 static struct request *__blk_mq_alloc_request(struct blk_mq_hw_ctx *hctx,
77 gfp_t gfp, bool reserved)
82 tag = blk_mq_get_tag(hctx->tags, gfp, reserved);
83 if (tag != BLK_MQ_TAG_FAIL) {
93 static int blk_mq_queue_enter(struct request_queue *q)
97 __percpu_counter_add(&q->mq_usage_counter, 1, 1000000);
99 /* we have problems to freeze the queue if it's initializing */
100 if (!blk_queue_bypass(q) || !blk_queue_init_done(q))
103 __percpu_counter_add(&q->mq_usage_counter, -1, 1000000);
105 spin_lock_irq(q->queue_lock);
106 ret = wait_event_interruptible_lock_irq(q->mq_freeze_wq,
107 !blk_queue_bypass(q) || blk_queue_dying(q),
109 /* inc usage with lock hold to avoid freeze_queue runs here */
110 if (!ret && !blk_queue_dying(q))
111 __percpu_counter_add(&q->mq_usage_counter, 1, 1000000);
112 else if (blk_queue_dying(q))
114 spin_unlock_irq(q->queue_lock);
119 static void blk_mq_queue_exit(struct request_queue *q)
121 __percpu_counter_add(&q->mq_usage_counter, -1, 1000000);
124 static void __blk_mq_drain_queue(struct request_queue *q)
129 spin_lock_irq(q->queue_lock);
130 count = percpu_counter_sum(&q->mq_usage_counter);
131 spin_unlock_irq(q->queue_lock);
135 blk_mq_run_queues(q, false);
141 * Guarantee no request is in use, so we can change any data structure of
142 * the queue afterward.
144 static void blk_mq_freeze_queue(struct request_queue *q)
148 spin_lock_irq(q->queue_lock);
149 drain = !q->bypass_depth++;
150 queue_flag_set(QUEUE_FLAG_BYPASS, q);
151 spin_unlock_irq(q->queue_lock);
154 __blk_mq_drain_queue(q);
157 void blk_mq_drain_queue(struct request_queue *q)
159 __blk_mq_drain_queue(q);
162 static void blk_mq_unfreeze_queue(struct request_queue *q)
166 spin_lock_irq(q->queue_lock);
167 if (!--q->bypass_depth) {
168 queue_flag_clear(QUEUE_FLAG_BYPASS, q);
171 WARN_ON_ONCE(q->bypass_depth < 0);
172 spin_unlock_irq(q->queue_lock);
174 wake_up_all(&q->mq_freeze_wq);
177 bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx)
179 return blk_mq_has_free_tags(hctx->tags);
181 EXPORT_SYMBOL(blk_mq_can_queue);
183 static void blk_mq_rq_ctx_init(struct request_queue *q, struct blk_mq_ctx *ctx,
184 struct request *rq, unsigned int rw_flags)
186 if (blk_queue_io_stat(q))
187 rw_flags |= REQ_IO_STAT;
190 rq->cmd_flags = rw_flags;
191 rq->start_time = jiffies;
192 set_start_time_ns(rq);
193 ctx->rq_dispatched[rw_is_sync(rw_flags)]++;
196 static struct request *blk_mq_alloc_request_pinned(struct request_queue *q,
203 struct blk_mq_ctx *ctx = blk_mq_get_ctx(q);
204 struct blk_mq_hw_ctx *hctx = q->mq_ops->map_queue(q, ctx->cpu);
206 rq = __blk_mq_alloc_request(hctx, gfp & ~__GFP_WAIT, reserved);
208 blk_mq_rq_ctx_init(q, ctx, rq, rw);
213 if (!(gfp & __GFP_WAIT))
216 __blk_mq_run_hw_queue(hctx);
217 blk_mq_wait_for_tags(hctx->tags);
223 struct request *blk_mq_alloc_request(struct request_queue *q, int rw, gfp_t gfp)
227 if (blk_mq_queue_enter(q))
230 rq = blk_mq_alloc_request_pinned(q, rw, gfp, false);
232 blk_mq_put_ctx(rq->mq_ctx);
236 struct request *blk_mq_alloc_reserved_request(struct request_queue *q, int rw,
241 if (blk_mq_queue_enter(q))
244 rq = blk_mq_alloc_request_pinned(q, rw, gfp, true);
246 blk_mq_put_ctx(rq->mq_ctx);
249 EXPORT_SYMBOL(blk_mq_alloc_reserved_request);
252 * Re-init and set pdu, if we have it
254 void blk_mq_rq_init(struct blk_mq_hw_ctx *hctx, struct request *rq)
256 blk_rq_init(hctx->queue, rq);
259 rq->special = blk_mq_rq_to_pdu(rq);
262 static void __blk_mq_free_request(struct blk_mq_hw_ctx *hctx,
263 struct blk_mq_ctx *ctx, struct request *rq)
265 const int tag = rq->tag;
266 struct request_queue *q = rq->q;
268 blk_mq_rq_init(hctx, rq);
269 blk_mq_put_tag(hctx->tags, tag);
271 blk_mq_queue_exit(q);
274 void blk_mq_free_request(struct request *rq)
276 struct blk_mq_ctx *ctx = rq->mq_ctx;
277 struct blk_mq_hw_ctx *hctx;
278 struct request_queue *q = rq->q;
280 ctx->rq_completed[rq_is_sync(rq)]++;
282 hctx = q->mq_ops->map_queue(q, ctx->cpu);
283 __blk_mq_free_request(hctx, ctx, rq);
286 static void blk_mq_bio_endio(struct request *rq, struct bio *bio, int error)
289 clear_bit(BIO_UPTODATE, &bio->bi_flags);
290 else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
293 if (unlikely(rq->cmd_flags & REQ_QUIET))
294 set_bit(BIO_QUIET, &bio->bi_flags);
296 /* don't actually finish bio if it's part of flush sequence */
297 if (!(rq->cmd_flags & REQ_FLUSH_SEQ))
298 bio_endio(bio, error);
301 void blk_mq_end_io(struct request *rq, int error)
303 struct bio *bio = rq->bio;
304 unsigned int bytes = 0;
306 trace_block_rq_complete(rq->q, rq);
309 struct bio *next = bio->bi_next;
312 bytes += bio->bi_iter.bi_size;
313 blk_mq_bio_endio(rq, bio, error);
317 blk_account_io_completion(rq, bytes);
319 blk_account_io_done(rq);
322 rq->end_io(rq, error);
324 blk_mq_free_request(rq);
326 EXPORT_SYMBOL(blk_mq_end_io);
328 static void __blk_mq_complete_request_remote(void *data)
330 struct request *rq = data;
332 rq->q->softirq_done_fn(rq);
335 void __blk_mq_complete_request(struct request *rq)
337 struct blk_mq_ctx *ctx = rq->mq_ctx;
340 if (!ctx->ipi_redirect) {
341 rq->q->softirq_done_fn(rq);
346 if (cpu != ctx->cpu && cpu_online(ctx->cpu)) {
347 rq->csd.func = __blk_mq_complete_request_remote;
350 __smp_call_function_single(ctx->cpu, &rq->csd, 0);
352 rq->q->softirq_done_fn(rq);
358 * blk_mq_complete_request - end I/O on a request
359 * @rq: the request being processed
362 * Ends all I/O on a request. It does not handle partial completions.
363 * The actual completion happens out-of-order, through a IPI handler.
365 void blk_mq_complete_request(struct request *rq)
367 if (unlikely(blk_should_fake_timeout(rq->q)))
369 if (!blk_mark_rq_complete(rq))
370 __blk_mq_complete_request(rq);
372 EXPORT_SYMBOL(blk_mq_complete_request);
374 static void blk_mq_start_request(struct request *rq, bool last)
376 struct request_queue *q = rq->q;
378 trace_block_rq_issue(q, rq);
381 * Just mark start time and set the started bit. Due to memory
382 * ordering, we know we'll see the correct deadline as long as
383 * REQ_ATOMIC_STARTED is seen.
385 rq->deadline = jiffies + q->rq_timeout;
386 set_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
388 if (q->dma_drain_size && blk_rq_bytes(rq)) {
390 * Make sure space for the drain appears. We know we can do
391 * this because max_hw_segments has been adjusted to be one
392 * fewer than the device can handle.
394 rq->nr_phys_segments++;
398 * Flag the last request in the series so that drivers know when IO
399 * should be kicked off, if they don't do it on a per-request basis.
401 * Note: the flag isn't the only condition drivers should do kick off.
402 * If drive is busy, the last request might not have the bit set.
405 rq->cmd_flags |= REQ_END;
408 static void blk_mq_requeue_request(struct request *rq)
410 struct request_queue *q = rq->q;
412 trace_block_rq_requeue(q, rq);
413 clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
415 rq->cmd_flags &= ~REQ_END;
417 if (q->dma_drain_size && blk_rq_bytes(rq))
418 rq->nr_phys_segments--;
421 struct blk_mq_timeout_data {
422 struct blk_mq_hw_ctx *hctx;
424 unsigned int *next_set;
427 static void blk_mq_timeout_check(void *__data, unsigned long *free_tags)
429 struct blk_mq_timeout_data *data = __data;
430 struct blk_mq_hw_ctx *hctx = data->hctx;
433 /* It may not be in flight yet (this is where
434 * the REQ_ATOMIC_STARTED flag comes in). The requests are
435 * statically allocated, so we know it's always safe to access the
436 * memory associated with a bit offset into ->rqs[].
442 tag = find_next_zero_bit(free_tags, hctx->queue_depth, tag);
443 if (tag >= hctx->queue_depth)
446 rq = hctx->rqs[tag++];
448 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
451 blk_rq_check_expired(rq, data->next, data->next_set);
455 static void blk_mq_hw_ctx_check_timeout(struct blk_mq_hw_ctx *hctx,
457 unsigned int *next_set)
459 struct blk_mq_timeout_data data = {
462 .next_set = next_set,
466 * Ask the tagging code to iterate busy requests, so we can
467 * check them for timeout.
469 blk_mq_tag_busy_iter(hctx->tags, blk_mq_timeout_check, &data);
472 static void blk_mq_rq_timer(unsigned long data)
474 struct request_queue *q = (struct request_queue *) data;
475 struct blk_mq_hw_ctx *hctx;
476 unsigned long next = 0;
479 queue_for_each_hw_ctx(q, hctx, i)
480 blk_mq_hw_ctx_check_timeout(hctx, &next, &next_set);
483 mod_timer(&q->timeout, round_jiffies_up(next));
487 * Reverse check our software queue for entries that we could potentially
488 * merge with. Currently includes a hand-wavy stop count of 8, to not spend
489 * too much time checking for merges.
491 static bool blk_mq_attempt_merge(struct request_queue *q,
492 struct blk_mq_ctx *ctx, struct bio *bio)
497 list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) {
503 if (!blk_rq_merge_ok(rq, bio))
506 el_ret = blk_try_merge(rq, bio);
507 if (el_ret == ELEVATOR_BACK_MERGE) {
508 if (bio_attempt_back_merge(q, rq, bio)) {
513 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
514 if (bio_attempt_front_merge(q, rq, bio)) {
525 void blk_mq_add_timer(struct request *rq)
527 __blk_add_timer(rq, NULL);
531 * Run this hardware queue, pulling any software queues mapped to it in.
532 * Note that this function currently has various problems around ordering
533 * of IO. In particular, we'd like FIFO behaviour on handling existing
534 * items on the hctx->dispatch list. Ignore that for now.
536 static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
538 struct request_queue *q = hctx->queue;
539 struct blk_mq_ctx *ctx;
544 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->flags)))
550 * Touch any software queue that has pending entries.
552 for_each_set_bit(bit, hctx->ctx_map, hctx->nr_ctx) {
553 clear_bit(bit, hctx->ctx_map);
554 ctx = hctx->ctxs[bit];
555 BUG_ON(bit != ctx->index_hw);
557 spin_lock(&ctx->lock);
558 list_splice_tail_init(&ctx->rq_list, &rq_list);
559 spin_unlock(&ctx->lock);
563 * If we have previous entries on our dispatch list, grab them
564 * and stuff them at the front for more fair dispatch.
566 if (!list_empty_careful(&hctx->dispatch)) {
567 spin_lock(&hctx->lock);
568 if (!list_empty(&hctx->dispatch))
569 list_splice_init(&hctx->dispatch, &rq_list);
570 spin_unlock(&hctx->lock);
574 * Delete and return all entries from our dispatch list
579 * Now process all the entries, sending them to the driver.
581 while (!list_empty(&rq_list)) {
584 rq = list_first_entry(&rq_list, struct request, queuelist);
585 list_del_init(&rq->queuelist);
587 blk_mq_start_request(rq, list_empty(&rq_list));
589 ret = q->mq_ops->queue_rq(hctx, rq);
591 case BLK_MQ_RQ_QUEUE_OK:
594 case BLK_MQ_RQ_QUEUE_BUSY:
596 * FIXME: we should have a mechanism to stop the queue
597 * like blk_stop_queue, otherwise we will waste cpu
600 list_add(&rq->queuelist, &rq_list);
601 blk_mq_requeue_request(rq);
604 pr_err("blk-mq: bad return on queue: %d\n", ret);
605 case BLK_MQ_RQ_QUEUE_ERROR:
607 blk_mq_end_io(rq, rq->errors);
611 if (ret == BLK_MQ_RQ_QUEUE_BUSY)
616 hctx->dispatched[0]++;
617 else if (queued < (1 << (BLK_MQ_MAX_DISPATCH_ORDER - 1)))
618 hctx->dispatched[ilog2(queued) + 1]++;
621 * Any items that need requeuing? Stuff them into hctx->dispatch,
622 * that is where we will continue on next queue run.
624 if (!list_empty(&rq_list)) {
625 spin_lock(&hctx->lock);
626 list_splice(&rq_list, &hctx->dispatch);
627 spin_unlock(&hctx->lock);
631 void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
633 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->flags)))
637 __blk_mq_run_hw_queue(hctx);
639 struct request_queue *q = hctx->queue;
641 kblockd_schedule_delayed_work(q, &hctx->delayed_work, 0);
645 void blk_mq_run_queues(struct request_queue *q, bool async)
647 struct blk_mq_hw_ctx *hctx;
650 queue_for_each_hw_ctx(q, hctx, i) {
651 if ((!blk_mq_hctx_has_pending(hctx) &&
652 list_empty_careful(&hctx->dispatch)) ||
653 test_bit(BLK_MQ_S_STOPPED, &hctx->flags))
656 blk_mq_run_hw_queue(hctx, async);
659 EXPORT_SYMBOL(blk_mq_run_queues);
661 void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
663 cancel_delayed_work(&hctx->delayed_work);
664 set_bit(BLK_MQ_S_STOPPED, &hctx->state);
666 EXPORT_SYMBOL(blk_mq_stop_hw_queue);
668 void blk_mq_stop_hw_queues(struct request_queue *q)
670 struct blk_mq_hw_ctx *hctx;
673 queue_for_each_hw_ctx(q, hctx, i)
674 blk_mq_stop_hw_queue(hctx);
676 EXPORT_SYMBOL(blk_mq_stop_hw_queues);
678 void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
680 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
681 __blk_mq_run_hw_queue(hctx);
683 EXPORT_SYMBOL(blk_mq_start_hw_queue);
685 void blk_mq_start_stopped_hw_queues(struct request_queue *q)
687 struct blk_mq_hw_ctx *hctx;
690 queue_for_each_hw_ctx(q, hctx, i) {
691 if (!test_bit(BLK_MQ_S_STOPPED, &hctx->state))
694 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
695 blk_mq_run_hw_queue(hctx, true);
698 EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
700 static void blk_mq_work_fn(struct work_struct *work)
702 struct blk_mq_hw_ctx *hctx;
704 hctx = container_of(work, struct blk_mq_hw_ctx, delayed_work.work);
705 __blk_mq_run_hw_queue(hctx);
708 static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx,
709 struct request *rq, bool at_head)
711 struct blk_mq_ctx *ctx = rq->mq_ctx;
713 trace_block_rq_insert(hctx->queue, rq);
716 list_add(&rq->queuelist, &ctx->rq_list);
718 list_add_tail(&rq->queuelist, &ctx->rq_list);
719 blk_mq_hctx_mark_pending(hctx, ctx);
722 * We do this early, to ensure we are on the right CPU.
724 blk_mq_add_timer(rq);
727 void blk_mq_insert_request(struct request_queue *q, struct request *rq,
728 bool at_head, bool run_queue)
730 struct blk_mq_hw_ctx *hctx;
731 struct blk_mq_ctx *ctx, *current_ctx;
734 hctx = q->mq_ops->map_queue(q, ctx->cpu);
736 if (rq->cmd_flags & (REQ_FLUSH | REQ_FUA)) {
737 blk_insert_flush(rq);
739 current_ctx = blk_mq_get_ctx(q);
741 if (!cpu_online(ctx->cpu)) {
743 hctx = q->mq_ops->map_queue(q, ctx->cpu);
746 spin_lock(&ctx->lock);
747 __blk_mq_insert_request(hctx, rq, at_head);
748 spin_unlock(&ctx->lock);
750 blk_mq_put_ctx(current_ctx);
754 __blk_mq_run_hw_queue(hctx);
756 EXPORT_SYMBOL(blk_mq_insert_request);
759 * This is a special version of blk_mq_insert_request to bypass FLUSH request
760 * check. Should only be used internally.
762 void blk_mq_run_request(struct request *rq, bool run_queue, bool async)
764 struct request_queue *q = rq->q;
765 struct blk_mq_hw_ctx *hctx;
766 struct blk_mq_ctx *ctx, *current_ctx;
768 current_ctx = blk_mq_get_ctx(q);
771 if (!cpu_online(ctx->cpu)) {
775 hctx = q->mq_ops->map_queue(q, ctx->cpu);
777 /* ctx->cpu might be offline */
778 spin_lock(&ctx->lock);
779 __blk_mq_insert_request(hctx, rq, false);
780 spin_unlock(&ctx->lock);
782 blk_mq_put_ctx(current_ctx);
785 blk_mq_run_hw_queue(hctx, async);
788 static void blk_mq_insert_requests(struct request_queue *q,
789 struct blk_mq_ctx *ctx,
790 struct list_head *list,
795 struct blk_mq_hw_ctx *hctx;
796 struct blk_mq_ctx *current_ctx;
798 trace_block_unplug(q, depth, !from_schedule);
800 current_ctx = blk_mq_get_ctx(q);
802 if (!cpu_online(ctx->cpu))
804 hctx = q->mq_ops->map_queue(q, ctx->cpu);
807 * preemption doesn't flush plug list, so it's possible ctx->cpu is
810 spin_lock(&ctx->lock);
811 while (!list_empty(list)) {
814 rq = list_first_entry(list, struct request, queuelist);
815 list_del_init(&rq->queuelist);
817 __blk_mq_insert_request(hctx, rq, false);
819 spin_unlock(&ctx->lock);
821 blk_mq_put_ctx(current_ctx);
823 blk_mq_run_hw_queue(hctx, from_schedule);
826 static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b)
828 struct request *rqa = container_of(a, struct request, queuelist);
829 struct request *rqb = container_of(b, struct request, queuelist);
831 return !(rqa->mq_ctx < rqb->mq_ctx ||
832 (rqa->mq_ctx == rqb->mq_ctx &&
833 blk_rq_pos(rqa) < blk_rq_pos(rqb)));
836 void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
838 struct blk_mq_ctx *this_ctx;
839 struct request_queue *this_q;
845 list_splice_init(&plug->mq_list, &list);
847 list_sort(NULL, &list, plug_ctx_cmp);
853 while (!list_empty(&list)) {
854 rq = list_entry_rq(list.next);
855 list_del_init(&rq->queuelist);
857 if (rq->mq_ctx != this_ctx) {
859 blk_mq_insert_requests(this_q, this_ctx,
864 this_ctx = rq->mq_ctx;
870 list_add_tail(&rq->queuelist, &ctx_list);
874 * If 'this_ctx' is set, we know we have entries to complete
875 * on 'ctx_list'. Do those.
878 blk_mq_insert_requests(this_q, this_ctx, &ctx_list, depth,
883 static void blk_mq_bio_to_request(struct request *rq, struct bio *bio)
885 init_request_from_bio(rq, bio);
886 blk_account_io_start(rq, 1);
889 static void blk_mq_make_request(struct request_queue *q, struct bio *bio)
891 struct blk_mq_hw_ctx *hctx;
892 struct blk_mq_ctx *ctx;
893 const int is_sync = rw_is_sync(bio->bi_rw);
894 const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
895 int rw = bio_data_dir(bio);
897 unsigned int use_plug, request_count = 0;
900 * If we have multiple hardware queues, just go directly to
901 * one of those for sync IO.
903 use_plug = !is_flush_fua && ((q->nr_hw_queues == 1) || !is_sync);
905 blk_queue_bounce(q, &bio);
907 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
908 bio_endio(bio, -EIO);
912 if (use_plug && blk_attempt_plug_merge(q, bio, &request_count))
915 if (blk_mq_queue_enter(q)) {
916 bio_endio(bio, -EIO);
920 ctx = blk_mq_get_ctx(q);
921 hctx = q->mq_ops->map_queue(q, ctx->cpu);
923 trace_block_getrq(q, bio, rw);
924 rq = __blk_mq_alloc_request(hctx, GFP_ATOMIC, false);
926 blk_mq_rq_ctx_init(q, ctx, rq, rw);
929 trace_block_sleeprq(q, bio, rw);
930 rq = blk_mq_alloc_request_pinned(q, rw, __GFP_WAIT|GFP_ATOMIC,
933 hctx = q->mq_ops->map_queue(q, ctx->cpu);
938 if (unlikely(is_flush_fua)) {
939 blk_mq_bio_to_request(rq, bio);
941 blk_insert_flush(rq);
946 * A task plug currently exists. Since this is completely lockless,
947 * utilize that to temporarily store requests until the task is
948 * either done or scheduled away.
951 struct blk_plug *plug = current->plug;
954 blk_mq_bio_to_request(rq, bio);
955 if (list_empty(&plug->mq_list))
957 else if (request_count >= BLK_MAX_REQUEST_COUNT) {
958 blk_flush_plug_list(plug, false);
961 list_add_tail(&rq->queuelist, &plug->mq_list);
967 spin_lock(&ctx->lock);
969 if ((hctx->flags & BLK_MQ_F_SHOULD_MERGE) &&
970 blk_mq_attempt_merge(q, ctx, bio))
971 __blk_mq_free_request(hctx, ctx, rq);
973 blk_mq_bio_to_request(rq, bio);
974 __blk_mq_insert_request(hctx, rq, false);
977 spin_unlock(&ctx->lock);
981 * For a SYNC request, send it to the hardware immediately. For an
982 * ASYNC request, just ensure that we run it later on. The latter
983 * allows for merging opportunities and more efficient dispatching.
986 blk_mq_run_hw_queue(hctx, !is_sync || is_flush_fua);
990 * Default mapping to a software queue, since we use one per CPU.
992 struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q, const int cpu)
994 return q->queue_hw_ctx[q->mq_map[cpu]];
996 EXPORT_SYMBOL(blk_mq_map_queue);
998 struct blk_mq_hw_ctx *blk_mq_alloc_single_hw_queue(struct blk_mq_reg *reg,
999 unsigned int hctx_index)
1001 return kmalloc_node(sizeof(struct blk_mq_hw_ctx),
1002 GFP_KERNEL | __GFP_ZERO, reg->numa_node);
1004 EXPORT_SYMBOL(blk_mq_alloc_single_hw_queue);
1006 void blk_mq_free_single_hw_queue(struct blk_mq_hw_ctx *hctx,
1007 unsigned int hctx_index)
1011 EXPORT_SYMBOL(blk_mq_free_single_hw_queue);
1013 static void blk_mq_hctx_notify(void *data, unsigned long action,
1016 struct blk_mq_hw_ctx *hctx = data;
1017 struct blk_mq_ctx *ctx;
1020 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
1024 * Move ctx entries to new CPU, if this one is going away.
1026 ctx = __blk_mq_get_ctx(hctx->queue, cpu);
1028 spin_lock(&ctx->lock);
1029 if (!list_empty(&ctx->rq_list)) {
1030 list_splice_init(&ctx->rq_list, &tmp);
1031 clear_bit(ctx->index_hw, hctx->ctx_map);
1033 spin_unlock(&ctx->lock);
1035 if (list_empty(&tmp))
1038 ctx = blk_mq_get_ctx(hctx->queue);
1039 spin_lock(&ctx->lock);
1041 while (!list_empty(&tmp)) {
1044 rq = list_first_entry(&tmp, struct request, queuelist);
1046 list_move_tail(&rq->queuelist, &ctx->rq_list);
1049 blk_mq_hctx_mark_pending(hctx, ctx);
1051 spin_unlock(&ctx->lock);
1052 blk_mq_put_ctx(ctx);
1055 static void blk_mq_init_hw_commands(struct blk_mq_hw_ctx *hctx,
1056 void (*init)(void *, struct blk_mq_hw_ctx *,
1057 struct request *, unsigned int),
1062 for (i = 0; i < hctx->queue_depth; i++) {
1063 struct request *rq = hctx->rqs[i];
1065 init(data, hctx, rq, i);
1069 void blk_mq_init_commands(struct request_queue *q,
1070 void (*init)(void *, struct blk_mq_hw_ctx *,
1071 struct request *, unsigned int),
1074 struct blk_mq_hw_ctx *hctx;
1077 queue_for_each_hw_ctx(q, hctx, i)
1078 blk_mq_init_hw_commands(hctx, init, data);
1080 EXPORT_SYMBOL(blk_mq_init_commands);
1082 static void blk_mq_free_rq_map(struct blk_mq_hw_ctx *hctx)
1086 while (!list_empty(&hctx->page_list)) {
1087 page = list_first_entry(&hctx->page_list, struct page, lru);
1088 list_del_init(&page->lru);
1089 __free_pages(page, page->private);
1095 blk_mq_free_tags(hctx->tags);
1098 static size_t order_to_size(unsigned int order)
1100 size_t ret = PAGE_SIZE;
1108 static int blk_mq_init_rq_map(struct blk_mq_hw_ctx *hctx,
1109 unsigned int reserved_tags, int node)
1111 unsigned int i, j, entries_per_page, max_order = 4;
1112 size_t rq_size, left;
1114 INIT_LIST_HEAD(&hctx->page_list);
1116 hctx->rqs = kmalloc_node(hctx->queue_depth * sizeof(struct request *),
1122 * rq_size is the size of the request plus driver payload, rounded
1123 * to the cacheline size
1125 rq_size = round_up(sizeof(struct request) + hctx->cmd_size,
1127 left = rq_size * hctx->queue_depth;
1129 for (i = 0; i < hctx->queue_depth;) {
1130 int this_order = max_order;
1135 while (left < order_to_size(this_order - 1) && this_order)
1139 page = alloc_pages_node(node, GFP_KERNEL, this_order);
1144 if (order_to_size(this_order) < rq_size)
1151 page->private = this_order;
1152 list_add_tail(&page->lru, &hctx->page_list);
1154 p = page_address(page);
1155 entries_per_page = order_to_size(this_order) / rq_size;
1156 to_do = min(entries_per_page, hctx->queue_depth - i);
1157 left -= to_do * rq_size;
1158 for (j = 0; j < to_do; j++) {
1160 blk_mq_rq_init(hctx, hctx->rqs[i]);
1166 if (i < (reserved_tags + BLK_MQ_TAG_MIN))
1168 else if (i != hctx->queue_depth) {
1169 hctx->queue_depth = i;
1170 pr_warn("%s: queue depth set to %u because of low memory\n",
1174 hctx->tags = blk_mq_init_tags(hctx->queue_depth, reserved_tags, node);
1177 blk_mq_free_rq_map(hctx);
1184 static int blk_mq_init_hw_queues(struct request_queue *q,
1185 struct blk_mq_reg *reg, void *driver_data)
1187 struct blk_mq_hw_ctx *hctx;
1191 * Initialize hardware queues
1193 queue_for_each_hw_ctx(q, hctx, i) {
1194 unsigned int num_maps;
1197 node = hctx->numa_node;
1198 if (node == NUMA_NO_NODE)
1199 node = hctx->numa_node = reg->numa_node;
1201 INIT_DELAYED_WORK(&hctx->delayed_work, blk_mq_work_fn);
1202 spin_lock_init(&hctx->lock);
1203 INIT_LIST_HEAD(&hctx->dispatch);
1205 hctx->queue_num = i;
1206 hctx->flags = reg->flags;
1207 hctx->queue_depth = reg->queue_depth;
1208 hctx->cmd_size = reg->cmd_size;
1210 blk_mq_init_cpu_notifier(&hctx->cpu_notifier,
1211 blk_mq_hctx_notify, hctx);
1212 blk_mq_register_cpu_notifier(&hctx->cpu_notifier);
1214 if (blk_mq_init_rq_map(hctx, reg->reserved_tags, node))
1218 * Allocate space for all possible cpus to avoid allocation in
1221 hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *),
1226 num_maps = ALIGN(nr_cpu_ids, BITS_PER_LONG) / BITS_PER_LONG;
1227 hctx->ctx_map = kzalloc_node(num_maps * sizeof(unsigned long),
1232 hctx->nr_ctx_map = num_maps;
1235 if (reg->ops->init_hctx &&
1236 reg->ops->init_hctx(hctx, driver_data, i))
1240 if (i == q->nr_hw_queues)
1246 queue_for_each_hw_ctx(q, hctx, j) {
1250 if (reg->ops->exit_hctx)
1251 reg->ops->exit_hctx(hctx, j);
1253 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
1254 blk_mq_free_rq_map(hctx);
1261 static void blk_mq_init_cpu_queues(struct request_queue *q,
1262 unsigned int nr_hw_queues)
1266 for_each_possible_cpu(i) {
1267 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
1268 struct blk_mq_hw_ctx *hctx;
1270 memset(__ctx, 0, sizeof(*__ctx));
1272 spin_lock_init(&__ctx->lock);
1273 INIT_LIST_HEAD(&__ctx->rq_list);
1276 /* If the cpu isn't online, the cpu is mapped to first hctx */
1277 hctx = q->mq_ops->map_queue(q, i);
1284 * Set local node, IFF we have more than one hw queue. If
1285 * not, we remain on the home node of the device
1287 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
1288 hctx->numa_node = cpu_to_node(i);
1292 static void blk_mq_map_swqueue(struct request_queue *q)
1295 struct blk_mq_hw_ctx *hctx;
1296 struct blk_mq_ctx *ctx;
1298 queue_for_each_hw_ctx(q, hctx, i) {
1303 * Map software to hardware queues
1305 queue_for_each_ctx(q, ctx, i) {
1306 /* If the cpu isn't online, the cpu is mapped to first hctx */
1307 hctx = q->mq_ops->map_queue(q, i);
1308 ctx->index_hw = hctx->nr_ctx;
1309 hctx->ctxs[hctx->nr_ctx++] = ctx;
1313 struct request_queue *blk_mq_init_queue(struct blk_mq_reg *reg,
1316 struct blk_mq_hw_ctx **hctxs;
1317 struct blk_mq_ctx *ctx;
1318 struct request_queue *q;
1321 if (!reg->nr_hw_queues ||
1322 !reg->ops->queue_rq || !reg->ops->map_queue ||
1323 !reg->ops->alloc_hctx || !reg->ops->free_hctx)
1324 return ERR_PTR(-EINVAL);
1326 if (!reg->queue_depth)
1327 reg->queue_depth = BLK_MQ_MAX_DEPTH;
1328 else if (reg->queue_depth > BLK_MQ_MAX_DEPTH) {
1329 pr_err("blk-mq: queuedepth too large (%u)\n", reg->queue_depth);
1330 reg->queue_depth = BLK_MQ_MAX_DEPTH;
1333 if (reg->queue_depth < (reg->reserved_tags + BLK_MQ_TAG_MIN))
1334 return ERR_PTR(-EINVAL);
1336 ctx = alloc_percpu(struct blk_mq_ctx);
1338 return ERR_PTR(-ENOMEM);
1340 hctxs = kmalloc_node(reg->nr_hw_queues * sizeof(*hctxs), GFP_KERNEL,
1346 for (i = 0; i < reg->nr_hw_queues; i++) {
1347 hctxs[i] = reg->ops->alloc_hctx(reg, i);
1351 hctxs[i]->numa_node = NUMA_NO_NODE;
1352 hctxs[i]->queue_num = i;
1355 q = blk_alloc_queue_node(GFP_KERNEL, reg->numa_node);
1359 q->mq_map = blk_mq_make_queue_map(reg);
1363 setup_timer(&q->timeout, blk_mq_rq_timer, (unsigned long) q);
1364 blk_queue_rq_timeout(q, 30000);
1366 q->nr_queues = nr_cpu_ids;
1367 q->nr_hw_queues = reg->nr_hw_queues;
1370 q->queue_hw_ctx = hctxs;
1372 q->mq_ops = reg->ops;
1373 q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
1375 q->sg_reserved_size = INT_MAX;
1377 blk_queue_make_request(q, blk_mq_make_request);
1378 blk_queue_rq_timed_out(q, reg->ops->timeout);
1380 blk_queue_rq_timeout(q, reg->timeout);
1382 if (reg->ops->complete)
1383 blk_queue_softirq_done(q, reg->ops->complete);
1385 blk_mq_init_flush(q);
1386 blk_mq_init_cpu_queues(q, reg->nr_hw_queues);
1388 q->flush_rq = kzalloc(round_up(sizeof(struct request) + reg->cmd_size,
1389 cache_line_size()), GFP_KERNEL);
1393 if (blk_mq_init_hw_queues(q, reg, driver_data))
1396 blk_mq_map_swqueue(q);
1398 mutex_lock(&all_q_mutex);
1399 list_add_tail(&q->all_q_node, &all_q_list);
1400 mutex_unlock(&all_q_mutex);
1409 blk_cleanup_queue(q);
1411 for (i = 0; i < reg->nr_hw_queues; i++) {
1414 reg->ops->free_hctx(hctxs[i], i);
1419 return ERR_PTR(-ENOMEM);
1421 EXPORT_SYMBOL(blk_mq_init_queue);
1423 void blk_mq_free_queue(struct request_queue *q)
1425 struct blk_mq_hw_ctx *hctx;
1428 queue_for_each_hw_ctx(q, hctx, i) {
1429 kfree(hctx->ctx_map);
1431 blk_mq_free_rq_map(hctx);
1432 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
1433 if (q->mq_ops->exit_hctx)
1434 q->mq_ops->exit_hctx(hctx, i);
1435 q->mq_ops->free_hctx(hctx, i);
1438 free_percpu(q->queue_ctx);
1439 kfree(q->queue_hw_ctx);
1442 q->queue_ctx = NULL;
1443 q->queue_hw_ctx = NULL;
1446 mutex_lock(&all_q_mutex);
1447 list_del_init(&q->all_q_node);
1448 mutex_unlock(&all_q_mutex);
1451 /* Basically redo blk_mq_init_queue with queue frozen */
1452 static void blk_mq_queue_reinit(struct request_queue *q)
1454 blk_mq_freeze_queue(q);
1456 blk_mq_update_queue_map(q->mq_map, q->nr_hw_queues);
1459 * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
1460 * we should change hctx numa_node according to new topology (this
1461 * involves free and re-allocate memory, worthy doing?)
1464 blk_mq_map_swqueue(q);
1466 blk_mq_unfreeze_queue(q);
1469 static int blk_mq_queue_reinit_notify(struct notifier_block *nb,
1470 unsigned long action, void *hcpu)
1472 struct request_queue *q;
1475 * Before new mapping is established, hotadded cpu might already start
1476 * handling requests. This doesn't break anything as we map offline
1477 * CPUs to first hardware queue. We will re-init queue below to get
1480 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN &&
1481 action != CPU_ONLINE && action != CPU_ONLINE_FROZEN)
1484 mutex_lock(&all_q_mutex);
1485 list_for_each_entry(q, &all_q_list, all_q_node)
1486 blk_mq_queue_reinit(q);
1487 mutex_unlock(&all_q_mutex);
1491 static int __init blk_mq_init(void)
1495 /* Must be called after percpu_counter_hotcpu_callback() */
1496 hotcpu_notifier(blk_mq_queue_reinit_notify, -10);
1500 subsys_initcall(blk_mq_init);