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 DEFINE_PER_CPU(struct llist_head, ipi_lists);
32 static struct blk_mq_ctx *__blk_mq_get_ctx(struct request_queue *q,
35 return per_cpu_ptr(q->queue_ctx, cpu);
39 * This assumes per-cpu software queueing queues. They could be per-node
40 * as well, for instance. For now this is hardcoded as-is. Note that we don't
41 * care about preemption, since we know the ctx's are persistent. This does
42 * mean that we can't rely on ctx always matching the currently running CPU.
44 static struct blk_mq_ctx *blk_mq_get_ctx(struct request_queue *q)
46 return __blk_mq_get_ctx(q, get_cpu());
49 static void blk_mq_put_ctx(struct blk_mq_ctx *ctx)
55 * Check if any of the ctx's have pending work in this hardware queue
57 static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx)
61 for (i = 0; i < hctx->nr_ctx_map; i++)
69 * Mark this ctx as having pending work in this hardware queue
71 static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx,
72 struct blk_mq_ctx *ctx)
74 if (!test_bit(ctx->index_hw, hctx->ctx_map))
75 set_bit(ctx->index_hw, hctx->ctx_map);
78 static struct request *blk_mq_alloc_rq(struct blk_mq_hw_ctx *hctx, gfp_t gfp,
84 tag = blk_mq_get_tag(hctx->tags, gfp, reserved);
85 if (tag != BLK_MQ_TAG_FAIL) {
95 static int blk_mq_queue_enter(struct request_queue *q)
99 __percpu_counter_add(&q->mq_usage_counter, 1, 1000000);
101 /* we have problems to freeze the queue if it's initializing */
102 if (!blk_queue_bypass(q) || !blk_queue_init_done(q))
105 __percpu_counter_add(&q->mq_usage_counter, -1, 1000000);
107 spin_lock_irq(q->queue_lock);
108 ret = wait_event_interruptible_lock_irq(q->mq_freeze_wq,
109 !blk_queue_bypass(q), *q->queue_lock);
110 /* inc usage with lock hold to avoid freeze_queue runs here */
112 __percpu_counter_add(&q->mq_usage_counter, 1, 1000000);
113 spin_unlock_irq(q->queue_lock);
118 static void blk_mq_queue_exit(struct request_queue *q)
120 __percpu_counter_add(&q->mq_usage_counter, -1, 1000000);
124 * Guarantee no request is in use, so we can change any data structure of
125 * the queue afterward.
127 static void blk_mq_freeze_queue(struct request_queue *q)
131 spin_lock_irq(q->queue_lock);
132 drain = !q->bypass_depth++;
133 queue_flag_set(QUEUE_FLAG_BYPASS, q);
134 spin_unlock_irq(q->queue_lock);
142 spin_lock_irq(q->queue_lock);
143 count = percpu_counter_sum(&q->mq_usage_counter);
144 spin_unlock_irq(q->queue_lock);
148 blk_mq_run_queues(q, false);
153 static void blk_mq_unfreeze_queue(struct request_queue *q)
157 spin_lock_irq(q->queue_lock);
158 if (!--q->bypass_depth) {
159 queue_flag_clear(QUEUE_FLAG_BYPASS, q);
162 WARN_ON_ONCE(q->bypass_depth < 0);
163 spin_unlock_irq(q->queue_lock);
165 wake_up_all(&q->mq_freeze_wq);
168 bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx)
170 return blk_mq_has_free_tags(hctx->tags);
172 EXPORT_SYMBOL(blk_mq_can_queue);
174 static void blk_mq_rq_ctx_init(struct blk_mq_ctx *ctx, struct request *rq,
175 unsigned int rw_flags)
178 rq->cmd_flags = rw_flags;
179 ctx->rq_dispatched[rw_is_sync(rw_flags)]++;
182 static struct request *__blk_mq_alloc_request(struct blk_mq_hw_ctx *hctx,
183 gfp_t gfp, bool reserved)
185 return blk_mq_alloc_rq(hctx, gfp, reserved);
188 static struct request *blk_mq_alloc_request_pinned(struct request_queue *q,
195 struct blk_mq_ctx *ctx = blk_mq_get_ctx(q);
196 struct blk_mq_hw_ctx *hctx = q->mq_ops->map_queue(q, ctx->cpu);
198 rq = __blk_mq_alloc_request(hctx, gfp & ~__GFP_WAIT, reserved);
200 blk_mq_rq_ctx_init(ctx, rq, rw);
202 } else if (!(gfp & __GFP_WAIT))
206 __blk_mq_run_hw_queue(hctx);
207 blk_mq_wait_for_tags(hctx->tags);
213 struct request *blk_mq_alloc_request(struct request_queue *q, int rw, gfp_t gfp)
217 if (blk_mq_queue_enter(q))
220 rq = blk_mq_alloc_request_pinned(q, rw, gfp, false);
221 blk_mq_put_ctx(rq->mq_ctx);
225 struct request *blk_mq_alloc_reserved_request(struct request_queue *q, int rw,
230 if (blk_mq_queue_enter(q))
233 rq = blk_mq_alloc_request_pinned(q, rw, gfp, true);
234 blk_mq_put_ctx(rq->mq_ctx);
237 EXPORT_SYMBOL(blk_mq_alloc_reserved_request);
240 * Re-init and set pdu, if we have it
242 static void blk_mq_rq_init(struct blk_mq_hw_ctx *hctx, struct request *rq)
244 blk_rq_init(hctx->queue, rq);
247 rq->special = blk_mq_rq_to_pdu(rq);
250 static void __blk_mq_free_request(struct blk_mq_hw_ctx *hctx,
251 struct blk_mq_ctx *ctx, struct request *rq)
253 const int tag = rq->tag;
254 struct request_queue *q = rq->q;
256 blk_mq_rq_init(hctx, rq);
257 blk_mq_put_tag(hctx->tags, tag);
259 blk_mq_queue_exit(q);
262 void blk_mq_free_request(struct request *rq)
264 struct blk_mq_ctx *ctx = rq->mq_ctx;
265 struct blk_mq_hw_ctx *hctx;
266 struct request_queue *q = rq->q;
268 ctx->rq_completed[rq_is_sync(rq)]++;
270 hctx = q->mq_ops->map_queue(q, ctx->cpu);
271 __blk_mq_free_request(hctx, ctx, rq);
274 static void blk_mq_bio_endio(struct request *rq, struct bio *bio, int error)
277 clear_bit(BIO_UPTODATE, &bio->bi_flags);
278 else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
281 if (unlikely(rq->cmd_flags & REQ_QUIET))
282 set_bit(BIO_QUIET, &bio->bi_flags);
284 /* don't actually finish bio if it's part of flush sequence */
285 if (!(rq->cmd_flags & REQ_FLUSH_SEQ))
286 bio_endio(bio, error);
289 void blk_mq_complete_request(struct request *rq, int error)
291 struct bio *bio = rq->bio;
292 unsigned int bytes = 0;
294 trace_block_rq_complete(rq->q, rq);
297 struct bio *next = bio->bi_next;
300 bytes += bio->bi_size;
301 blk_mq_bio_endio(rq, bio, error);
305 blk_account_io_completion(rq, bytes);
308 rq->end_io(rq, error);
310 blk_mq_free_request(rq);
312 blk_account_io_done(rq);
315 void __blk_mq_end_io(struct request *rq, int error)
317 if (!blk_mark_rq_complete(rq))
318 blk_mq_complete_request(rq, error);
321 #if defined(CONFIG_SMP) && defined(CONFIG_USE_GENERIC_SMP_HELPERS)
324 * Called with interrupts disabled.
326 static void ipi_end_io(void *data)
328 struct llist_head *list = &per_cpu(ipi_lists, smp_processor_id());
329 struct llist_node *entry, *next;
332 entry = llist_del_all(list);
336 rq = llist_entry(entry, struct request, ll_list);
337 __blk_mq_end_io(rq, rq->errors);
342 static int ipi_remote_cpu(struct blk_mq_ctx *ctx, const int cpu,
343 struct request *rq, const int error)
345 struct call_single_data *data = &rq->csd;
348 rq->ll_list.next = NULL;
351 * If the list is non-empty, an existing IPI must already
352 * be "in flight". If that is the case, we need not schedule
355 if (llist_add(&rq->ll_list, &per_cpu(ipi_lists, ctx->cpu))) {
356 data->func = ipi_end_io;
358 __smp_call_function_single(ctx->cpu, data, 0);
363 #else /* CONFIG_SMP && CONFIG_USE_GENERIC_SMP_HELPERS */
364 static int ipi_remote_cpu(struct blk_mq_ctx *ctx, const int cpu,
365 struct request *rq, const int error)
372 * End IO on this request on a multiqueue enabled driver. We'll either do
373 * it directly inline, or punt to a local IPI handler on the matching
376 void blk_mq_end_io(struct request *rq, int error)
378 struct blk_mq_ctx *ctx = rq->mq_ctx;
381 if (!ctx->ipi_redirect)
382 return __blk_mq_end_io(rq, error);
386 if (cpu == ctx->cpu || !cpu_online(ctx->cpu) ||
387 !ipi_remote_cpu(ctx, cpu, rq, error))
388 __blk_mq_end_io(rq, error);
392 EXPORT_SYMBOL(blk_mq_end_io);
394 static void blk_mq_start_request(struct request *rq)
396 struct request_queue *q = rq->q;
398 trace_block_rq_issue(q, rq);
401 * Just mark start time and set the started bit. Due to memory
402 * ordering, we know we'll see the correct deadline as long as
403 * REQ_ATOMIC_STARTED is seen.
405 rq->deadline = jiffies + q->rq_timeout;
406 set_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
409 static void blk_mq_requeue_request(struct request *rq)
411 struct request_queue *q = rq->q;
413 trace_block_rq_requeue(q, rq);
414 clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
417 struct blk_mq_timeout_data {
418 struct blk_mq_hw_ctx *hctx;
420 unsigned int *next_set;
423 static void blk_mq_timeout_check(void *__data, unsigned long *free_tags)
425 struct blk_mq_timeout_data *data = __data;
426 struct blk_mq_hw_ctx *hctx = data->hctx;
429 /* It may not be in flight yet (this is where
430 * the REQ_ATOMIC_STARTED flag comes in). The requests are
431 * statically allocated, so we know it's always safe to access the
432 * memory associated with a bit offset into ->rqs[].
438 tag = find_next_zero_bit(free_tags, hctx->queue_depth, tag);
439 if (tag >= hctx->queue_depth)
442 rq = hctx->rqs[tag++];
444 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
447 blk_rq_check_expired(rq, data->next, data->next_set);
451 static void blk_mq_hw_ctx_check_timeout(struct blk_mq_hw_ctx *hctx,
453 unsigned int *next_set)
455 struct blk_mq_timeout_data data = {
458 .next_set = next_set,
462 * Ask the tagging code to iterate busy requests, so we can
463 * check them for timeout.
465 blk_mq_tag_busy_iter(hctx->tags, blk_mq_timeout_check, &data);
468 static void blk_mq_rq_timer(unsigned long data)
470 struct request_queue *q = (struct request_queue *) data;
471 struct blk_mq_hw_ctx *hctx;
472 unsigned long next = 0;
475 queue_for_each_hw_ctx(q, hctx, i)
476 blk_mq_hw_ctx_check_timeout(hctx, &next, &next_set);
479 mod_timer(&q->timeout, round_jiffies_up(next));
483 * Reverse check our software queue for entries that we could potentially
484 * merge with. Currently includes a hand-wavy stop count of 8, to not spend
485 * too much time checking for merges.
487 static bool blk_mq_attempt_merge(struct request_queue *q,
488 struct blk_mq_ctx *ctx, struct bio *bio)
493 list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) {
499 if (!blk_rq_merge_ok(rq, bio))
502 el_ret = blk_try_merge(rq, bio);
503 if (el_ret == ELEVATOR_BACK_MERGE) {
504 if (bio_attempt_back_merge(q, rq, bio)) {
509 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
510 if (bio_attempt_front_merge(q, rq, bio)) {
521 void blk_mq_add_timer(struct request *rq)
523 __blk_add_timer(rq, NULL);
527 * Run this hardware queue, pulling any software queues mapped to it in.
528 * Note that this function currently has various problems around ordering
529 * of IO. In particular, we'd like FIFO behaviour on handling existing
530 * items on the hctx->dispatch list. Ignore that for now.
532 static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
534 struct request_queue *q = hctx->queue;
535 struct blk_mq_ctx *ctx;
540 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->flags)))
546 * Touch any software queue that has pending entries.
548 for_each_set_bit(bit, hctx->ctx_map, hctx->nr_ctx) {
549 clear_bit(bit, hctx->ctx_map);
550 ctx = hctx->ctxs[bit];
551 BUG_ON(bit != ctx->index_hw);
553 spin_lock(&ctx->lock);
554 list_splice_tail_init(&ctx->rq_list, &rq_list);
555 spin_unlock(&ctx->lock);
559 * If we have previous entries on our dispatch list, grab them
560 * and stuff them at the front for more fair dispatch.
562 if (!list_empty_careful(&hctx->dispatch)) {
563 spin_lock(&hctx->lock);
564 if (!list_empty(&hctx->dispatch))
565 list_splice_init(&hctx->dispatch, &rq_list);
566 spin_unlock(&hctx->lock);
570 * Delete and return all entries from our dispatch list
575 * Now process all the entries, sending them to the driver.
577 while (!list_empty(&rq_list)) {
580 rq = list_first_entry(&rq_list, struct request, queuelist);
581 list_del_init(&rq->queuelist);
582 blk_mq_start_request(rq);
585 * Last request in the series. Flag it as such, this
586 * enables drivers to know when IO should be kicked off,
587 * if they don't do it on a per-request basis.
589 * Note: the flag isn't the only condition drivers
590 * should do kick off. If drive is busy, the last
591 * request might not have the bit set.
593 if (list_empty(&rq_list))
594 rq->cmd_flags |= REQ_END;
596 ret = q->mq_ops->queue_rq(hctx, rq);
598 case BLK_MQ_RQ_QUEUE_OK:
601 case BLK_MQ_RQ_QUEUE_BUSY:
603 * FIXME: we should have a mechanism to stop the queue
604 * like blk_stop_queue, otherwise we will waste cpu
607 list_add(&rq->queuelist, &rq_list);
608 blk_mq_requeue_request(rq);
611 pr_err("blk-mq: bad return on queue: %d\n", ret);
613 case BLK_MQ_RQ_QUEUE_ERROR:
614 blk_mq_end_io(rq, rq->errors);
618 if (ret == BLK_MQ_RQ_QUEUE_BUSY)
623 hctx->dispatched[0]++;
624 else if (queued < (1 << (BLK_MQ_MAX_DISPATCH_ORDER - 1)))
625 hctx->dispatched[ilog2(queued) + 1]++;
628 * Any items that need requeuing? Stuff them into hctx->dispatch,
629 * that is where we will continue on next queue run.
631 if (!list_empty(&rq_list)) {
632 spin_lock(&hctx->lock);
633 list_splice(&rq_list, &hctx->dispatch);
634 spin_unlock(&hctx->lock);
638 void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
640 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->flags)))
644 __blk_mq_run_hw_queue(hctx);
646 struct request_queue *q = hctx->queue;
648 kblockd_schedule_delayed_work(q, &hctx->delayed_work, 0);
652 void blk_mq_run_queues(struct request_queue *q, bool async)
654 struct blk_mq_hw_ctx *hctx;
657 queue_for_each_hw_ctx(q, hctx, i) {
658 if ((!blk_mq_hctx_has_pending(hctx) &&
659 list_empty_careful(&hctx->dispatch)) ||
660 test_bit(BLK_MQ_S_STOPPED, &hctx->flags))
663 blk_mq_run_hw_queue(hctx, async);
666 EXPORT_SYMBOL(blk_mq_run_queues);
668 void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
670 cancel_delayed_work(&hctx->delayed_work);
671 set_bit(BLK_MQ_S_STOPPED, &hctx->state);
673 EXPORT_SYMBOL(blk_mq_stop_hw_queue);
675 void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
677 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
678 __blk_mq_run_hw_queue(hctx);
680 EXPORT_SYMBOL(blk_mq_start_hw_queue);
682 void blk_mq_start_stopped_hw_queues(struct request_queue *q)
684 struct blk_mq_hw_ctx *hctx;
687 queue_for_each_hw_ctx(q, hctx, i) {
688 if (!test_bit(BLK_MQ_S_STOPPED, &hctx->state))
691 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
692 blk_mq_run_hw_queue(hctx, true);
695 EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
697 static void blk_mq_work_fn(struct work_struct *work)
699 struct blk_mq_hw_ctx *hctx;
701 hctx = container_of(work, struct blk_mq_hw_ctx, delayed_work.work);
702 __blk_mq_run_hw_queue(hctx);
705 static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx,
708 struct blk_mq_ctx *ctx = rq->mq_ctx;
710 list_add_tail(&rq->queuelist, &ctx->rq_list);
711 blk_mq_hctx_mark_pending(hctx, ctx);
714 * We do this early, to ensure we are on the right CPU.
716 blk_mq_add_timer(rq);
719 void blk_mq_insert_request(struct request_queue *q, struct request *rq,
722 struct blk_mq_hw_ctx *hctx;
723 struct blk_mq_ctx *ctx, *current_ctx;
726 hctx = q->mq_ops->map_queue(q, ctx->cpu);
728 if (rq->cmd_flags & (REQ_FLUSH | REQ_FUA)) {
729 blk_insert_flush(rq);
731 current_ctx = blk_mq_get_ctx(q);
733 if (!cpu_online(ctx->cpu)) {
735 hctx = q->mq_ops->map_queue(q, ctx->cpu);
738 spin_lock(&ctx->lock);
739 __blk_mq_insert_request(hctx, rq);
740 spin_unlock(&ctx->lock);
742 blk_mq_put_ctx(current_ctx);
746 __blk_mq_run_hw_queue(hctx);
748 EXPORT_SYMBOL(blk_mq_insert_request);
751 * This is a special version of blk_mq_insert_request to bypass FLUSH request
752 * check. Should only be used internally.
754 void blk_mq_run_request(struct request *rq, bool run_queue, bool async)
756 struct request_queue *q = rq->q;
757 struct blk_mq_hw_ctx *hctx;
758 struct blk_mq_ctx *ctx, *current_ctx;
760 current_ctx = blk_mq_get_ctx(q);
763 if (!cpu_online(ctx->cpu)) {
767 hctx = q->mq_ops->map_queue(q, ctx->cpu);
769 /* ctx->cpu might be offline */
770 spin_lock(&ctx->lock);
771 __blk_mq_insert_request(hctx, rq);
772 spin_unlock(&ctx->lock);
774 blk_mq_put_ctx(current_ctx);
777 blk_mq_run_hw_queue(hctx, async);
780 static void blk_mq_insert_requests(struct request_queue *q,
781 struct blk_mq_ctx *ctx,
782 struct list_head *list,
787 struct blk_mq_hw_ctx *hctx;
788 struct blk_mq_ctx *current_ctx;
790 trace_block_unplug(q, depth, !from_schedule);
792 current_ctx = blk_mq_get_ctx(q);
794 if (!cpu_online(ctx->cpu))
796 hctx = q->mq_ops->map_queue(q, ctx->cpu);
799 * preemption doesn't flush plug list, so it's possible ctx->cpu is
802 spin_lock(&ctx->lock);
803 while (!list_empty(list)) {
806 rq = list_first_entry(list, struct request, queuelist);
807 list_del_init(&rq->queuelist);
809 __blk_mq_insert_request(hctx, rq);
811 spin_unlock(&ctx->lock);
813 blk_mq_put_ctx(current_ctx);
815 blk_mq_run_hw_queue(hctx, from_schedule);
818 static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b)
820 struct request *rqa = container_of(a, struct request, queuelist);
821 struct request *rqb = container_of(b, struct request, queuelist);
823 return !(rqa->mq_ctx < rqb->mq_ctx ||
824 (rqa->mq_ctx == rqb->mq_ctx &&
825 blk_rq_pos(rqa) < blk_rq_pos(rqb)));
828 void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
830 struct blk_mq_ctx *this_ctx;
831 struct request_queue *this_q;
837 list_splice_init(&plug->mq_list, &list);
839 list_sort(NULL, &list, plug_ctx_cmp);
845 while (!list_empty(&list)) {
846 rq = list_entry_rq(list.next);
847 list_del_init(&rq->queuelist);
849 if (rq->mq_ctx != this_ctx) {
851 blk_mq_insert_requests(this_q, this_ctx,
856 this_ctx = rq->mq_ctx;
862 list_add_tail(&rq->queuelist, &ctx_list);
866 * If 'this_ctx' is set, we know we have entries to complete
867 * on 'ctx_list'. Do those.
870 blk_mq_insert_requests(this_q, this_ctx, &ctx_list, depth,
875 static void blk_mq_bio_to_request(struct request *rq, struct bio *bio)
877 init_request_from_bio(rq, bio);
878 blk_account_io_start(rq, 1);
881 static void blk_mq_make_request(struct request_queue *q, struct bio *bio)
883 struct blk_mq_hw_ctx *hctx;
884 struct blk_mq_ctx *ctx;
885 const int is_sync = rw_is_sync(bio->bi_rw);
886 const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
887 int rw = bio_data_dir(bio);
889 unsigned int use_plug, request_count = 0;
892 * If we have multiple hardware queues, just go directly to
893 * one of those for sync IO.
895 use_plug = !is_flush_fua && ((q->nr_hw_queues == 1) || !is_sync);
897 blk_queue_bounce(q, &bio);
899 if (use_plug && blk_attempt_plug_merge(q, bio, &request_count))
902 if (blk_mq_queue_enter(q)) {
903 bio_endio(bio, -EIO);
907 ctx = blk_mq_get_ctx(q);
908 hctx = q->mq_ops->map_queue(q, ctx->cpu);
910 trace_block_getrq(q, bio, rw);
911 rq = __blk_mq_alloc_request(hctx, GFP_ATOMIC, false);
913 blk_mq_rq_ctx_init(ctx, rq, rw);
916 trace_block_sleeprq(q, bio, rw);
917 rq = blk_mq_alloc_request_pinned(q, rw, __GFP_WAIT|GFP_ATOMIC,
920 hctx = q->mq_ops->map_queue(q, ctx->cpu);
925 if (unlikely(is_flush_fua)) {
926 blk_mq_bio_to_request(rq, bio);
928 blk_insert_flush(rq);
933 * A task plug currently exists. Since this is completely lockless,
934 * utilize that to temporarily store requests until the task is
935 * either done or scheduled away.
938 struct blk_plug *plug = current->plug;
941 blk_mq_bio_to_request(rq, bio);
942 if (list_empty(&plug->list))
944 else if (request_count >= BLK_MAX_REQUEST_COUNT) {
945 blk_flush_plug_list(plug, false);
948 list_add_tail(&rq->queuelist, &plug->mq_list);
954 spin_lock(&ctx->lock);
956 if ((hctx->flags & BLK_MQ_F_SHOULD_MERGE) &&
957 blk_mq_attempt_merge(q, ctx, bio))
958 __blk_mq_free_request(hctx, ctx, rq);
960 blk_mq_bio_to_request(rq, bio);
961 __blk_mq_insert_request(hctx, rq);
964 spin_unlock(&ctx->lock);
968 * For a SYNC request, send it to the hardware immediately. For an
969 * ASYNC request, just ensure that we run it later on. The latter
970 * allows for merging opportunities and more efficient dispatching.
973 blk_mq_run_hw_queue(hctx, !is_sync || is_flush_fua);
977 * Default mapping to a software queue, since we use one per CPU.
979 struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q, const int cpu)
981 return q->queue_hw_ctx[q->mq_map[cpu]];
983 EXPORT_SYMBOL(blk_mq_map_queue);
985 struct blk_mq_hw_ctx *blk_mq_alloc_single_hw_queue(struct blk_mq_reg *reg,
986 unsigned int hctx_index)
988 return kmalloc_node(sizeof(struct blk_mq_hw_ctx),
989 GFP_KERNEL | __GFP_ZERO, reg->numa_node);
991 EXPORT_SYMBOL(blk_mq_alloc_single_hw_queue);
993 void blk_mq_free_single_hw_queue(struct blk_mq_hw_ctx *hctx,
994 unsigned int hctx_index)
998 EXPORT_SYMBOL(blk_mq_free_single_hw_queue);
1000 static void blk_mq_hctx_notify(void *data, unsigned long action,
1003 struct blk_mq_hw_ctx *hctx = data;
1004 struct blk_mq_ctx *ctx;
1007 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
1011 * Move ctx entries to new CPU, if this one is going away.
1013 ctx = __blk_mq_get_ctx(hctx->queue, cpu);
1015 spin_lock(&ctx->lock);
1016 if (!list_empty(&ctx->rq_list)) {
1017 list_splice_init(&ctx->rq_list, &tmp);
1018 clear_bit(ctx->index_hw, hctx->ctx_map);
1020 spin_unlock(&ctx->lock);
1022 if (list_empty(&tmp))
1025 ctx = blk_mq_get_ctx(hctx->queue);
1026 spin_lock(&ctx->lock);
1028 while (!list_empty(&tmp)) {
1031 rq = list_first_entry(&tmp, struct request, queuelist);
1033 list_move_tail(&rq->queuelist, &ctx->rq_list);
1036 blk_mq_hctx_mark_pending(hctx, ctx);
1038 spin_unlock(&ctx->lock);
1039 blk_mq_put_ctx(ctx);
1042 static void blk_mq_init_hw_commands(struct blk_mq_hw_ctx *hctx,
1043 void (*init)(void *, struct blk_mq_hw_ctx *,
1044 struct request *, unsigned int),
1049 for (i = 0; i < hctx->queue_depth; i++) {
1050 struct request *rq = hctx->rqs[i];
1052 init(data, hctx, rq, i);
1056 void blk_mq_init_commands(struct request_queue *q,
1057 void (*init)(void *, struct blk_mq_hw_ctx *,
1058 struct request *, unsigned int),
1061 struct blk_mq_hw_ctx *hctx;
1064 queue_for_each_hw_ctx(q, hctx, i)
1065 blk_mq_init_hw_commands(hctx, init, data);
1067 EXPORT_SYMBOL(blk_mq_init_commands);
1069 static void blk_mq_free_rq_map(struct blk_mq_hw_ctx *hctx)
1073 while (!list_empty(&hctx->page_list)) {
1074 page = list_first_entry(&hctx->page_list, struct page, list);
1075 list_del_init(&page->list);
1076 __free_pages(page, page->private);
1082 blk_mq_free_tags(hctx->tags);
1085 static size_t order_to_size(unsigned int order)
1087 size_t ret = PAGE_SIZE;
1095 static int blk_mq_init_rq_map(struct blk_mq_hw_ctx *hctx,
1096 unsigned int reserved_tags, int node)
1098 unsigned int i, j, entries_per_page, max_order = 4;
1099 size_t rq_size, left;
1101 INIT_LIST_HEAD(&hctx->page_list);
1103 hctx->rqs = kmalloc_node(hctx->queue_depth * sizeof(struct request *),
1109 * rq_size is the size of the request plus driver payload, rounded
1110 * to the cacheline size
1112 rq_size = round_up(sizeof(struct request) + hctx->cmd_size,
1114 left = rq_size * hctx->queue_depth;
1116 for (i = 0; i < hctx->queue_depth;) {
1117 int this_order = max_order;
1122 while (left < order_to_size(this_order - 1) && this_order)
1126 page = alloc_pages_node(node, GFP_KERNEL, this_order);
1131 if (order_to_size(this_order) < rq_size)
1138 page->private = this_order;
1139 list_add_tail(&page->list, &hctx->page_list);
1141 p = page_address(page);
1142 entries_per_page = order_to_size(this_order) / rq_size;
1143 to_do = min(entries_per_page, hctx->queue_depth - i);
1144 left -= to_do * rq_size;
1145 for (j = 0; j < to_do; j++) {
1147 blk_mq_rq_init(hctx, hctx->rqs[i]);
1153 if (i < (reserved_tags + BLK_MQ_TAG_MIN))
1155 else if (i != hctx->queue_depth) {
1156 hctx->queue_depth = i;
1157 pr_warn("%s: queue depth set to %u because of low memory\n",
1161 hctx->tags = blk_mq_init_tags(hctx->queue_depth, reserved_tags, node);
1164 blk_mq_free_rq_map(hctx);
1171 static int blk_mq_init_hw_queues(struct request_queue *q,
1172 struct blk_mq_reg *reg, void *driver_data)
1174 struct blk_mq_hw_ctx *hctx;
1178 * Initialize hardware queues
1180 queue_for_each_hw_ctx(q, hctx, i) {
1181 unsigned int num_maps;
1184 node = hctx->numa_node;
1185 if (node == NUMA_NO_NODE)
1186 node = hctx->numa_node = reg->numa_node;
1188 INIT_DELAYED_WORK(&hctx->delayed_work, blk_mq_work_fn);
1189 spin_lock_init(&hctx->lock);
1190 INIT_LIST_HEAD(&hctx->dispatch);
1192 hctx->queue_num = i;
1193 hctx->flags = reg->flags;
1194 hctx->queue_depth = reg->queue_depth;
1195 hctx->cmd_size = reg->cmd_size;
1197 blk_mq_init_cpu_notifier(&hctx->cpu_notifier,
1198 blk_mq_hctx_notify, hctx);
1199 blk_mq_register_cpu_notifier(&hctx->cpu_notifier);
1201 if (blk_mq_init_rq_map(hctx, reg->reserved_tags, node))
1205 * Allocate space for all possible cpus to avoid allocation in
1208 hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *),
1213 num_maps = ALIGN(nr_cpu_ids, BITS_PER_LONG) / BITS_PER_LONG;
1214 hctx->ctx_map = kzalloc_node(num_maps * sizeof(unsigned long),
1219 hctx->nr_ctx_map = num_maps;
1222 if (reg->ops->init_hctx &&
1223 reg->ops->init_hctx(hctx, driver_data, i))
1227 if (i == q->nr_hw_queues)
1233 queue_for_each_hw_ctx(q, hctx, j) {
1237 if (reg->ops->exit_hctx)
1238 reg->ops->exit_hctx(hctx, j);
1240 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
1241 blk_mq_free_rq_map(hctx);
1248 static void blk_mq_init_cpu_queues(struct request_queue *q,
1249 unsigned int nr_hw_queues)
1253 for_each_possible_cpu(i) {
1254 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
1255 struct blk_mq_hw_ctx *hctx;
1257 memset(__ctx, 0, sizeof(*__ctx));
1259 spin_lock_init(&__ctx->lock);
1260 INIT_LIST_HEAD(&__ctx->rq_list);
1263 /* If the cpu isn't online, the cpu is mapped to first hctx */
1264 hctx = q->mq_ops->map_queue(q, i);
1271 * Set local node, IFF we have more than one hw queue. If
1272 * not, we remain on the home node of the device
1274 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
1275 hctx->numa_node = cpu_to_node(i);
1279 static void blk_mq_map_swqueue(struct request_queue *q)
1282 struct blk_mq_hw_ctx *hctx;
1283 struct blk_mq_ctx *ctx;
1285 queue_for_each_hw_ctx(q, hctx, i) {
1290 * Map software to hardware queues
1292 queue_for_each_ctx(q, ctx, i) {
1293 /* If the cpu isn't online, the cpu is mapped to first hctx */
1294 hctx = q->mq_ops->map_queue(q, i);
1295 ctx->index_hw = hctx->nr_ctx;
1296 hctx->ctxs[hctx->nr_ctx++] = ctx;
1300 struct request_queue *blk_mq_init_queue(struct blk_mq_reg *reg,
1303 struct blk_mq_hw_ctx **hctxs;
1304 struct blk_mq_ctx *ctx;
1305 struct request_queue *q;
1308 if (!reg->nr_hw_queues ||
1309 !reg->ops->queue_rq || !reg->ops->map_queue ||
1310 !reg->ops->alloc_hctx || !reg->ops->free_hctx)
1311 return ERR_PTR(-EINVAL);
1313 if (!reg->queue_depth)
1314 reg->queue_depth = BLK_MQ_MAX_DEPTH;
1315 else if (reg->queue_depth > BLK_MQ_MAX_DEPTH) {
1316 pr_err("blk-mq: queuedepth too large (%u)\n", reg->queue_depth);
1317 reg->queue_depth = BLK_MQ_MAX_DEPTH;
1320 if (reg->queue_depth < (reg->reserved_tags + BLK_MQ_TAG_MIN))
1321 return ERR_PTR(-EINVAL);
1323 ctx = alloc_percpu(struct blk_mq_ctx);
1325 return ERR_PTR(-ENOMEM);
1327 hctxs = kmalloc_node(reg->nr_hw_queues * sizeof(*hctxs), GFP_KERNEL,
1333 for (i = 0; i < reg->nr_hw_queues; i++) {
1334 hctxs[i] = reg->ops->alloc_hctx(reg, i);
1338 hctxs[i]->numa_node = NUMA_NO_NODE;
1339 hctxs[i]->queue_num = i;
1342 q = blk_alloc_queue_node(GFP_KERNEL, reg->numa_node);
1346 q->mq_map = blk_mq_make_queue_map(reg);
1350 setup_timer(&q->timeout, blk_mq_rq_timer, (unsigned long) q);
1351 blk_queue_rq_timeout(q, 30000);
1353 q->nr_queues = nr_cpu_ids;
1354 q->nr_hw_queues = reg->nr_hw_queues;
1357 q->queue_hw_ctx = hctxs;
1359 q->mq_ops = reg->ops;
1361 blk_queue_make_request(q, blk_mq_make_request);
1362 blk_queue_rq_timed_out(q, reg->ops->timeout);
1364 blk_queue_rq_timeout(q, reg->timeout);
1366 blk_mq_init_flush(q);
1367 blk_mq_init_cpu_queues(q, reg->nr_hw_queues);
1369 if (blk_mq_init_hw_queues(q, reg, driver_data))
1372 blk_mq_map_swqueue(q);
1374 mutex_lock(&all_q_mutex);
1375 list_add_tail(&q->all_q_node, &all_q_list);
1376 mutex_unlock(&all_q_mutex);
1382 blk_cleanup_queue(q);
1384 for (i = 0; i < reg->nr_hw_queues; i++) {
1387 reg->ops->free_hctx(hctxs[i], i);
1392 return ERR_PTR(-ENOMEM);
1394 EXPORT_SYMBOL(blk_mq_init_queue);
1396 void blk_mq_free_queue(struct request_queue *q)
1398 struct blk_mq_hw_ctx *hctx;
1401 queue_for_each_hw_ctx(q, hctx, i) {
1402 cancel_delayed_work_sync(&hctx->delayed_work);
1403 kfree(hctx->ctx_map);
1405 blk_mq_free_rq_map(hctx);
1406 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
1407 if (q->mq_ops->exit_hctx)
1408 q->mq_ops->exit_hctx(hctx, i);
1409 q->mq_ops->free_hctx(hctx, i);
1412 free_percpu(q->queue_ctx);
1413 kfree(q->queue_hw_ctx);
1416 q->queue_ctx = NULL;
1417 q->queue_hw_ctx = NULL;
1420 mutex_lock(&all_q_mutex);
1421 list_del_init(&q->all_q_node);
1422 mutex_unlock(&all_q_mutex);
1424 EXPORT_SYMBOL(blk_mq_free_queue);
1426 /* Basically redo blk_mq_init_queue with queue frozen */
1427 static void __cpuinit blk_mq_queue_reinit(struct request_queue *q)
1429 blk_mq_freeze_queue(q);
1431 blk_mq_update_queue_map(q->mq_map, q->nr_hw_queues);
1434 * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
1435 * we should change hctx numa_node according to new topology (this
1436 * involves free and re-allocate memory, worthy doing?)
1439 blk_mq_map_swqueue(q);
1441 blk_mq_unfreeze_queue(q);
1444 static int __cpuinit blk_mq_queue_reinit_notify(struct notifier_block *nb,
1445 unsigned long action, void *hcpu)
1447 struct request_queue *q;
1450 * Before new mapping is established, hotadded cpu might already start
1451 * handling requests. This doesn't break anything as we map offline
1452 * CPUs to first hardware queue. We will re-init queue below to get
1455 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN &&
1456 action != CPU_ONLINE && action != CPU_ONLINE_FROZEN)
1459 mutex_lock(&all_q_mutex);
1460 list_for_each_entry(q, &all_q_list, all_q_node)
1461 blk_mq_queue_reinit(q);
1462 mutex_unlock(&all_q_mutex);
1466 static int __init blk_mq_init(void)
1470 for_each_possible_cpu(i)
1471 init_llist_head(&per_cpu(ipi_lists, i));
1475 /* Must be called after percpu_counter_hotcpu_callback() */
1476 hotcpu_notifier(blk_mq_queue_reinit_notify, -10);
1480 subsys_initcall(blk_mq_init);