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 request_queue *q, struct blk_mq_ctx *ctx,
175 struct request *rq, unsigned int rw_flags)
177 if (blk_queue_io_stat(q))
178 rw_flags |= REQ_IO_STAT;
181 rq->cmd_flags = rw_flags;
182 ctx->rq_dispatched[rw_is_sync(rw_flags)]++;
185 static struct request *__blk_mq_alloc_request(struct blk_mq_hw_ctx *hctx,
186 gfp_t gfp, bool reserved)
188 return blk_mq_alloc_rq(hctx, gfp, reserved);
191 static struct request *blk_mq_alloc_request_pinned(struct request_queue *q,
198 struct blk_mq_ctx *ctx = blk_mq_get_ctx(q);
199 struct blk_mq_hw_ctx *hctx = q->mq_ops->map_queue(q, ctx->cpu);
201 rq = __blk_mq_alloc_request(hctx, gfp & ~__GFP_WAIT, reserved);
203 blk_mq_rq_ctx_init(q, ctx, rq, rw);
205 } else if (!(gfp & __GFP_WAIT))
209 __blk_mq_run_hw_queue(hctx);
210 blk_mq_wait_for_tags(hctx->tags);
216 struct request *blk_mq_alloc_request(struct request_queue *q, int rw,
217 gfp_t gfp, bool reserved)
221 if (blk_mq_queue_enter(q))
224 rq = blk_mq_alloc_request_pinned(q, rw, gfp, reserved);
225 blk_mq_put_ctx(rq->mq_ctx);
229 struct request *blk_mq_alloc_reserved_request(struct request_queue *q, int rw,
234 if (blk_mq_queue_enter(q))
237 rq = blk_mq_alloc_request_pinned(q, rw, gfp, true);
238 blk_mq_put_ctx(rq->mq_ctx);
241 EXPORT_SYMBOL(blk_mq_alloc_reserved_request);
244 * Re-init and set pdu, if we have it
246 static void blk_mq_rq_init(struct blk_mq_hw_ctx *hctx, struct request *rq)
248 blk_rq_init(hctx->queue, rq);
251 rq->special = blk_mq_rq_to_pdu(rq);
254 static void __blk_mq_free_request(struct blk_mq_hw_ctx *hctx,
255 struct blk_mq_ctx *ctx, struct request *rq)
257 const int tag = rq->tag;
258 struct request_queue *q = rq->q;
260 blk_mq_rq_init(hctx, rq);
261 blk_mq_put_tag(hctx->tags, tag);
263 blk_mq_queue_exit(q);
266 void blk_mq_free_request(struct request *rq)
268 struct blk_mq_ctx *ctx = rq->mq_ctx;
269 struct blk_mq_hw_ctx *hctx;
270 struct request_queue *q = rq->q;
272 ctx->rq_completed[rq_is_sync(rq)]++;
274 hctx = q->mq_ops->map_queue(q, ctx->cpu);
275 __blk_mq_free_request(hctx, ctx, rq);
278 static void blk_mq_bio_endio(struct request *rq, struct bio *bio, int error)
281 clear_bit(BIO_UPTODATE, &bio->bi_flags);
282 else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
285 if (unlikely(rq->cmd_flags & REQ_QUIET))
286 set_bit(BIO_QUIET, &bio->bi_flags);
288 /* don't actually finish bio if it's part of flush sequence */
289 if (!(rq->cmd_flags & REQ_FLUSH_SEQ))
290 bio_endio(bio, error);
293 void blk_mq_complete_request(struct request *rq, int error)
295 struct bio *bio = rq->bio;
296 unsigned int bytes = 0;
298 trace_block_rq_complete(rq->q, rq);
301 struct bio *next = bio->bi_next;
304 bytes += bio->bi_size;
305 blk_mq_bio_endio(rq, bio, error);
309 blk_account_io_completion(rq, bytes);
312 rq->end_io(rq, error);
314 blk_mq_free_request(rq);
316 blk_account_io_done(rq);
319 void __blk_mq_end_io(struct request *rq, int error)
321 if (!blk_mark_rq_complete(rq))
322 blk_mq_complete_request(rq, error);
325 #if defined(CONFIG_SMP)
328 * Called with interrupts disabled.
330 static void ipi_end_io(void *data)
332 struct llist_head *list = &per_cpu(ipi_lists, smp_processor_id());
333 struct llist_node *entry, *next;
336 entry = llist_del_all(list);
340 rq = llist_entry(entry, struct request, ll_list);
341 __blk_mq_end_io(rq, rq->errors);
346 static int ipi_remote_cpu(struct blk_mq_ctx *ctx, const int cpu,
347 struct request *rq, const int error)
349 struct call_single_data *data = &rq->csd;
352 rq->ll_list.next = NULL;
355 * If the list is non-empty, an existing IPI must already
356 * be "in flight". If that is the case, we need not schedule
359 if (llist_add(&rq->ll_list, &per_cpu(ipi_lists, ctx->cpu))) {
360 data->func = ipi_end_io;
362 __smp_call_function_single(ctx->cpu, data, 0);
367 #else /* CONFIG_SMP */
368 static int ipi_remote_cpu(struct blk_mq_ctx *ctx, const int cpu,
369 struct request *rq, const int error)
376 * End IO on this request on a multiqueue enabled driver. We'll either do
377 * it directly inline, or punt to a local IPI handler on the matching
380 void blk_mq_end_io(struct request *rq, int error)
382 struct blk_mq_ctx *ctx = rq->mq_ctx;
385 if (!ctx->ipi_redirect)
386 return __blk_mq_end_io(rq, error);
390 if (cpu == ctx->cpu || !cpu_online(ctx->cpu) ||
391 !ipi_remote_cpu(ctx, cpu, rq, error))
392 __blk_mq_end_io(rq, error);
396 EXPORT_SYMBOL(blk_mq_end_io);
398 static void blk_mq_start_request(struct request *rq)
400 struct request_queue *q = rq->q;
402 trace_block_rq_issue(q, rq);
405 * Just mark start time and set the started bit. Due to memory
406 * ordering, we know we'll see the correct deadline as long as
407 * REQ_ATOMIC_STARTED is seen.
409 rq->deadline = jiffies + q->rq_timeout;
410 set_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
413 static void blk_mq_requeue_request(struct request *rq)
415 struct request_queue *q = rq->q;
417 trace_block_rq_requeue(q, rq);
418 clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
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);
586 blk_mq_start_request(rq);
589 * Last request in the series. Flag it as such, this
590 * enables drivers to know when IO should be kicked off,
591 * if they don't do it on a per-request basis.
593 * Note: the flag isn't the only condition drivers
594 * should do kick off. If drive is busy, the last
595 * request might not have the bit set.
597 if (list_empty(&rq_list))
598 rq->cmd_flags |= REQ_END;
600 ret = q->mq_ops->queue_rq(hctx, rq);
602 case BLK_MQ_RQ_QUEUE_OK:
605 case BLK_MQ_RQ_QUEUE_BUSY:
607 * FIXME: we should have a mechanism to stop the queue
608 * like blk_stop_queue, otherwise we will waste cpu
611 list_add(&rq->queuelist, &rq_list);
612 blk_mq_requeue_request(rq);
615 pr_err("blk-mq: bad return on queue: %d\n", ret);
617 case BLK_MQ_RQ_QUEUE_ERROR:
618 blk_mq_end_io(rq, rq->errors);
622 if (ret == BLK_MQ_RQ_QUEUE_BUSY)
627 hctx->dispatched[0]++;
628 else if (queued < (1 << (BLK_MQ_MAX_DISPATCH_ORDER - 1)))
629 hctx->dispatched[ilog2(queued) + 1]++;
632 * Any items that need requeuing? Stuff them into hctx->dispatch,
633 * that is where we will continue on next queue run.
635 if (!list_empty(&rq_list)) {
636 spin_lock(&hctx->lock);
637 list_splice(&rq_list, &hctx->dispatch);
638 spin_unlock(&hctx->lock);
642 void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
644 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->flags)))
648 __blk_mq_run_hw_queue(hctx);
650 struct request_queue *q = hctx->queue;
652 kblockd_schedule_delayed_work(q, &hctx->delayed_work, 0);
656 void blk_mq_run_queues(struct request_queue *q, bool async)
658 struct blk_mq_hw_ctx *hctx;
661 queue_for_each_hw_ctx(q, hctx, i) {
662 if ((!blk_mq_hctx_has_pending(hctx) &&
663 list_empty_careful(&hctx->dispatch)) ||
664 test_bit(BLK_MQ_S_STOPPED, &hctx->flags))
667 blk_mq_run_hw_queue(hctx, async);
670 EXPORT_SYMBOL(blk_mq_run_queues);
672 void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
674 cancel_delayed_work(&hctx->delayed_work);
675 set_bit(BLK_MQ_S_STOPPED, &hctx->state);
677 EXPORT_SYMBOL(blk_mq_stop_hw_queue);
679 void blk_mq_stop_hw_queues(struct request_queue *q)
681 struct blk_mq_hw_ctx *hctx;
684 queue_for_each_hw_ctx(q, hctx, i)
685 blk_mq_stop_hw_queue(hctx);
687 EXPORT_SYMBOL(blk_mq_stop_hw_queues);
689 void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
691 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
692 __blk_mq_run_hw_queue(hctx);
694 EXPORT_SYMBOL(blk_mq_start_hw_queue);
696 void blk_mq_start_stopped_hw_queues(struct request_queue *q)
698 struct blk_mq_hw_ctx *hctx;
701 queue_for_each_hw_ctx(q, hctx, i) {
702 if (!test_bit(BLK_MQ_S_STOPPED, &hctx->state))
705 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
706 blk_mq_run_hw_queue(hctx, true);
709 EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
711 static void blk_mq_work_fn(struct work_struct *work)
713 struct blk_mq_hw_ctx *hctx;
715 hctx = container_of(work, struct blk_mq_hw_ctx, delayed_work.work);
716 __blk_mq_run_hw_queue(hctx);
719 static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx,
722 struct blk_mq_ctx *ctx = rq->mq_ctx;
724 list_add_tail(&rq->queuelist, &ctx->rq_list);
725 blk_mq_hctx_mark_pending(hctx, ctx);
728 * We do this early, to ensure we are on the right CPU.
730 blk_mq_add_timer(rq);
733 void blk_mq_insert_request(struct request_queue *q, struct request *rq,
736 struct blk_mq_hw_ctx *hctx;
737 struct blk_mq_ctx *ctx, *current_ctx;
740 hctx = q->mq_ops->map_queue(q, ctx->cpu);
742 if (rq->cmd_flags & (REQ_FLUSH | REQ_FUA)) {
743 blk_insert_flush(rq);
745 current_ctx = blk_mq_get_ctx(q);
747 if (!cpu_online(ctx->cpu)) {
749 hctx = q->mq_ops->map_queue(q, ctx->cpu);
752 spin_lock(&ctx->lock);
753 __blk_mq_insert_request(hctx, rq);
754 spin_unlock(&ctx->lock);
756 blk_mq_put_ctx(current_ctx);
760 __blk_mq_run_hw_queue(hctx);
762 EXPORT_SYMBOL(blk_mq_insert_request);
765 * This is a special version of blk_mq_insert_request to bypass FLUSH request
766 * check. Should only be used internally.
768 void blk_mq_run_request(struct request *rq, bool run_queue, bool async)
770 struct request_queue *q = rq->q;
771 struct blk_mq_hw_ctx *hctx;
772 struct blk_mq_ctx *ctx, *current_ctx;
774 current_ctx = blk_mq_get_ctx(q);
777 if (!cpu_online(ctx->cpu)) {
781 hctx = q->mq_ops->map_queue(q, ctx->cpu);
783 /* ctx->cpu might be offline */
784 spin_lock(&ctx->lock);
785 __blk_mq_insert_request(hctx, rq);
786 spin_unlock(&ctx->lock);
788 blk_mq_put_ctx(current_ctx);
791 blk_mq_run_hw_queue(hctx, async);
794 static void blk_mq_insert_requests(struct request_queue *q,
795 struct blk_mq_ctx *ctx,
796 struct list_head *list,
801 struct blk_mq_hw_ctx *hctx;
802 struct blk_mq_ctx *current_ctx;
804 trace_block_unplug(q, depth, !from_schedule);
806 current_ctx = blk_mq_get_ctx(q);
808 if (!cpu_online(ctx->cpu))
810 hctx = q->mq_ops->map_queue(q, ctx->cpu);
813 * preemption doesn't flush plug list, so it's possible ctx->cpu is
816 spin_lock(&ctx->lock);
817 while (!list_empty(list)) {
820 rq = list_first_entry(list, struct request, queuelist);
821 list_del_init(&rq->queuelist);
823 __blk_mq_insert_request(hctx, rq);
825 spin_unlock(&ctx->lock);
827 blk_mq_put_ctx(current_ctx);
829 blk_mq_run_hw_queue(hctx, from_schedule);
832 static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b)
834 struct request *rqa = container_of(a, struct request, queuelist);
835 struct request *rqb = container_of(b, struct request, queuelist);
837 return !(rqa->mq_ctx < rqb->mq_ctx ||
838 (rqa->mq_ctx == rqb->mq_ctx &&
839 blk_rq_pos(rqa) < blk_rq_pos(rqb)));
842 void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
844 struct blk_mq_ctx *this_ctx;
845 struct request_queue *this_q;
851 list_splice_init(&plug->mq_list, &list);
853 list_sort(NULL, &list, plug_ctx_cmp);
859 while (!list_empty(&list)) {
860 rq = list_entry_rq(list.next);
861 list_del_init(&rq->queuelist);
863 if (rq->mq_ctx != this_ctx) {
865 blk_mq_insert_requests(this_q, this_ctx,
870 this_ctx = rq->mq_ctx;
876 list_add_tail(&rq->queuelist, &ctx_list);
880 * If 'this_ctx' is set, we know we have entries to complete
881 * on 'ctx_list'. Do those.
884 blk_mq_insert_requests(this_q, this_ctx, &ctx_list, depth,
889 static void blk_mq_bio_to_request(struct request *rq, struct bio *bio)
891 init_request_from_bio(rq, bio);
892 blk_account_io_start(rq, 1);
895 static void blk_mq_make_request(struct request_queue *q, struct bio *bio)
897 struct blk_mq_hw_ctx *hctx;
898 struct blk_mq_ctx *ctx;
899 const int is_sync = rw_is_sync(bio->bi_rw);
900 const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
901 int rw = bio_data_dir(bio);
903 unsigned int use_plug, request_count = 0;
906 * If we have multiple hardware queues, just go directly to
907 * one of those for sync IO.
909 use_plug = !is_flush_fua && ((q->nr_hw_queues == 1) || !is_sync);
911 blk_queue_bounce(q, &bio);
913 if (use_plug && blk_attempt_plug_merge(q, bio, &request_count))
916 if (blk_mq_queue_enter(q)) {
917 bio_endio(bio, -EIO);
921 ctx = blk_mq_get_ctx(q);
922 hctx = q->mq_ops->map_queue(q, ctx->cpu);
924 trace_block_getrq(q, bio, rw);
925 rq = __blk_mq_alloc_request(hctx, GFP_ATOMIC, false);
927 blk_mq_rq_ctx_init(q, ctx, rq, rw);
930 trace_block_sleeprq(q, bio, rw);
931 rq = blk_mq_alloc_request_pinned(q, rw, __GFP_WAIT|GFP_ATOMIC,
934 hctx = q->mq_ops->map_queue(q, ctx->cpu);
939 if (unlikely(is_flush_fua)) {
940 blk_mq_bio_to_request(rq, bio);
942 blk_insert_flush(rq);
947 * A task plug currently exists. Since this is completely lockless,
948 * utilize that to temporarily store requests until the task is
949 * either done or scheduled away.
952 struct blk_plug *plug = current->plug;
955 blk_mq_bio_to_request(rq, bio);
956 if (list_empty(&plug->mq_list))
958 else if (request_count >= BLK_MAX_REQUEST_COUNT) {
959 blk_flush_plug_list(plug, false);
962 list_add_tail(&rq->queuelist, &plug->mq_list);
968 spin_lock(&ctx->lock);
970 if ((hctx->flags & BLK_MQ_F_SHOULD_MERGE) &&
971 blk_mq_attempt_merge(q, ctx, bio))
972 __blk_mq_free_request(hctx, ctx, rq);
974 blk_mq_bio_to_request(rq, bio);
975 __blk_mq_insert_request(hctx, rq);
978 spin_unlock(&ctx->lock);
982 * For a SYNC request, send it to the hardware immediately. For an
983 * ASYNC request, just ensure that we run it later on. The latter
984 * allows for merging opportunities and more efficient dispatching.
987 blk_mq_run_hw_queue(hctx, !is_sync || is_flush_fua);
991 * Default mapping to a software queue, since we use one per CPU.
993 struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q, const int cpu)
995 return q->queue_hw_ctx[q->mq_map[cpu]];
997 EXPORT_SYMBOL(blk_mq_map_queue);
999 struct blk_mq_hw_ctx *blk_mq_alloc_single_hw_queue(struct blk_mq_reg *reg,
1000 unsigned int hctx_index)
1002 return kmalloc_node(sizeof(struct blk_mq_hw_ctx),
1003 GFP_KERNEL | __GFP_ZERO, reg->numa_node);
1005 EXPORT_SYMBOL(blk_mq_alloc_single_hw_queue);
1007 void blk_mq_free_single_hw_queue(struct blk_mq_hw_ctx *hctx,
1008 unsigned int hctx_index)
1012 EXPORT_SYMBOL(blk_mq_free_single_hw_queue);
1014 static void blk_mq_hctx_notify(void *data, unsigned long action,
1017 struct blk_mq_hw_ctx *hctx = data;
1018 struct blk_mq_ctx *ctx;
1021 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
1025 * Move ctx entries to new CPU, if this one is going away.
1027 ctx = __blk_mq_get_ctx(hctx->queue, cpu);
1029 spin_lock(&ctx->lock);
1030 if (!list_empty(&ctx->rq_list)) {
1031 list_splice_init(&ctx->rq_list, &tmp);
1032 clear_bit(ctx->index_hw, hctx->ctx_map);
1034 spin_unlock(&ctx->lock);
1036 if (list_empty(&tmp))
1039 ctx = blk_mq_get_ctx(hctx->queue);
1040 spin_lock(&ctx->lock);
1042 while (!list_empty(&tmp)) {
1045 rq = list_first_entry(&tmp, struct request, queuelist);
1047 list_move_tail(&rq->queuelist, &ctx->rq_list);
1050 blk_mq_hctx_mark_pending(hctx, ctx);
1052 spin_unlock(&ctx->lock);
1053 blk_mq_put_ctx(ctx);
1056 static void blk_mq_init_hw_commands(struct blk_mq_hw_ctx *hctx,
1057 void (*init)(void *, struct blk_mq_hw_ctx *,
1058 struct request *, unsigned int),
1063 for (i = 0; i < hctx->queue_depth; i++) {
1064 struct request *rq = hctx->rqs[i];
1066 init(data, hctx, rq, i);
1070 void blk_mq_init_commands(struct request_queue *q,
1071 void (*init)(void *, struct blk_mq_hw_ctx *,
1072 struct request *, unsigned int),
1075 struct blk_mq_hw_ctx *hctx;
1078 queue_for_each_hw_ctx(q, hctx, i)
1079 blk_mq_init_hw_commands(hctx, init, data);
1081 EXPORT_SYMBOL(blk_mq_init_commands);
1083 static void blk_mq_free_rq_map(struct blk_mq_hw_ctx *hctx)
1087 while (!list_empty(&hctx->page_list)) {
1088 page = list_first_entry(&hctx->page_list, struct page, list);
1089 list_del_init(&page->list);
1090 __free_pages(page, page->private);
1096 blk_mq_free_tags(hctx->tags);
1099 static size_t order_to_size(unsigned int order)
1101 size_t ret = PAGE_SIZE;
1109 static int blk_mq_init_rq_map(struct blk_mq_hw_ctx *hctx,
1110 unsigned int reserved_tags, int node)
1112 unsigned int i, j, entries_per_page, max_order = 4;
1113 size_t rq_size, left;
1115 INIT_LIST_HEAD(&hctx->page_list);
1117 hctx->rqs = kmalloc_node(hctx->queue_depth * sizeof(struct request *),
1123 * rq_size is the size of the request plus driver payload, rounded
1124 * to the cacheline size
1126 rq_size = round_up(sizeof(struct request) + hctx->cmd_size,
1128 left = rq_size * hctx->queue_depth;
1130 for (i = 0; i < hctx->queue_depth;) {
1131 int this_order = max_order;
1136 while (left < order_to_size(this_order - 1) && this_order)
1140 page = alloc_pages_node(node, GFP_KERNEL, this_order);
1145 if (order_to_size(this_order) < rq_size)
1152 page->private = this_order;
1153 list_add_tail(&page->list, &hctx->page_list);
1155 p = page_address(page);
1156 entries_per_page = order_to_size(this_order) / rq_size;
1157 to_do = min(entries_per_page, hctx->queue_depth - i);
1158 left -= to_do * rq_size;
1159 for (j = 0; j < to_do; j++) {
1161 blk_mq_rq_init(hctx, hctx->rqs[i]);
1167 if (i < (reserved_tags + BLK_MQ_TAG_MIN))
1169 else if (i != hctx->queue_depth) {
1170 hctx->queue_depth = i;
1171 pr_warn("%s: queue depth set to %u because of low memory\n",
1175 hctx->tags = blk_mq_init_tags(hctx->queue_depth, reserved_tags, node);
1178 blk_mq_free_rq_map(hctx);
1185 static int blk_mq_init_hw_queues(struct request_queue *q,
1186 struct blk_mq_reg *reg, void *driver_data)
1188 struct blk_mq_hw_ctx *hctx;
1192 * Initialize hardware queues
1194 queue_for_each_hw_ctx(q, hctx, i) {
1195 unsigned int num_maps;
1198 node = hctx->numa_node;
1199 if (node == NUMA_NO_NODE)
1200 node = hctx->numa_node = reg->numa_node;
1202 INIT_DELAYED_WORK(&hctx->delayed_work, blk_mq_work_fn);
1203 spin_lock_init(&hctx->lock);
1204 INIT_LIST_HEAD(&hctx->dispatch);
1206 hctx->queue_num = i;
1207 hctx->flags = reg->flags;
1208 hctx->queue_depth = reg->queue_depth;
1209 hctx->cmd_size = reg->cmd_size;
1211 blk_mq_init_cpu_notifier(&hctx->cpu_notifier,
1212 blk_mq_hctx_notify, hctx);
1213 blk_mq_register_cpu_notifier(&hctx->cpu_notifier);
1215 if (blk_mq_init_rq_map(hctx, reg->reserved_tags, node))
1219 * Allocate space for all possible cpus to avoid allocation in
1222 hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *),
1227 num_maps = ALIGN(nr_cpu_ids, BITS_PER_LONG) / BITS_PER_LONG;
1228 hctx->ctx_map = kzalloc_node(num_maps * sizeof(unsigned long),
1233 hctx->nr_ctx_map = num_maps;
1236 if (reg->ops->init_hctx &&
1237 reg->ops->init_hctx(hctx, driver_data, i))
1241 if (i == q->nr_hw_queues)
1247 queue_for_each_hw_ctx(q, hctx, j) {
1251 if (reg->ops->exit_hctx)
1252 reg->ops->exit_hctx(hctx, j);
1254 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
1255 blk_mq_free_rq_map(hctx);
1262 static void blk_mq_init_cpu_queues(struct request_queue *q,
1263 unsigned int nr_hw_queues)
1267 for_each_possible_cpu(i) {
1268 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
1269 struct blk_mq_hw_ctx *hctx;
1271 memset(__ctx, 0, sizeof(*__ctx));
1273 spin_lock_init(&__ctx->lock);
1274 INIT_LIST_HEAD(&__ctx->rq_list);
1277 /* If the cpu isn't online, the cpu is mapped to first hctx */
1278 hctx = q->mq_ops->map_queue(q, i);
1285 * Set local node, IFF we have more than one hw queue. If
1286 * not, we remain on the home node of the device
1288 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
1289 hctx->numa_node = cpu_to_node(i);
1293 static void blk_mq_map_swqueue(struct request_queue *q)
1296 struct blk_mq_hw_ctx *hctx;
1297 struct blk_mq_ctx *ctx;
1299 queue_for_each_hw_ctx(q, hctx, i) {
1304 * Map software to hardware queues
1306 queue_for_each_ctx(q, ctx, i) {
1307 /* If the cpu isn't online, the cpu is mapped to first hctx */
1308 hctx = q->mq_ops->map_queue(q, i);
1309 ctx->index_hw = hctx->nr_ctx;
1310 hctx->ctxs[hctx->nr_ctx++] = ctx;
1314 struct request_queue *blk_mq_init_queue(struct blk_mq_reg *reg,
1317 struct blk_mq_hw_ctx **hctxs;
1318 struct blk_mq_ctx *ctx;
1319 struct request_queue *q;
1322 if (!reg->nr_hw_queues ||
1323 !reg->ops->queue_rq || !reg->ops->map_queue ||
1324 !reg->ops->alloc_hctx || !reg->ops->free_hctx)
1325 return ERR_PTR(-EINVAL);
1327 if (!reg->queue_depth)
1328 reg->queue_depth = BLK_MQ_MAX_DEPTH;
1329 else if (reg->queue_depth > BLK_MQ_MAX_DEPTH) {
1330 pr_err("blk-mq: queuedepth too large (%u)\n", reg->queue_depth);
1331 reg->queue_depth = BLK_MQ_MAX_DEPTH;
1335 * Set aside a tag for flush requests. It will only be used while
1336 * another flush request is in progress but outside the driver.
1338 * TODO: only allocate if flushes are supported
1341 reg->reserved_tags++;
1343 if (reg->queue_depth < (reg->reserved_tags + BLK_MQ_TAG_MIN))
1344 return ERR_PTR(-EINVAL);
1346 ctx = alloc_percpu(struct blk_mq_ctx);
1348 return ERR_PTR(-ENOMEM);
1350 hctxs = kmalloc_node(reg->nr_hw_queues * sizeof(*hctxs), GFP_KERNEL,
1356 for (i = 0; i < reg->nr_hw_queues; i++) {
1357 hctxs[i] = reg->ops->alloc_hctx(reg, i);
1361 hctxs[i]->numa_node = NUMA_NO_NODE;
1362 hctxs[i]->queue_num = i;
1365 q = blk_alloc_queue_node(GFP_KERNEL, reg->numa_node);
1369 q->mq_map = blk_mq_make_queue_map(reg);
1373 setup_timer(&q->timeout, blk_mq_rq_timer, (unsigned long) q);
1374 blk_queue_rq_timeout(q, 30000);
1376 q->nr_queues = nr_cpu_ids;
1377 q->nr_hw_queues = reg->nr_hw_queues;
1380 q->queue_hw_ctx = hctxs;
1382 q->mq_ops = reg->ops;
1383 q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
1385 blk_queue_make_request(q, blk_mq_make_request);
1386 blk_queue_rq_timed_out(q, reg->ops->timeout);
1388 blk_queue_rq_timeout(q, reg->timeout);
1390 blk_mq_init_flush(q);
1391 blk_mq_init_cpu_queues(q, reg->nr_hw_queues);
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);
1406 blk_cleanup_queue(q);
1408 for (i = 0; i < reg->nr_hw_queues; i++) {
1411 reg->ops->free_hctx(hctxs[i], i);
1416 return ERR_PTR(-ENOMEM);
1418 EXPORT_SYMBOL(blk_mq_init_queue);
1420 void blk_mq_free_queue(struct request_queue *q)
1422 struct blk_mq_hw_ctx *hctx;
1425 queue_for_each_hw_ctx(q, hctx, i) {
1426 cancel_delayed_work_sync(&hctx->delayed_work);
1427 kfree(hctx->ctx_map);
1429 blk_mq_free_rq_map(hctx);
1430 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
1431 if (q->mq_ops->exit_hctx)
1432 q->mq_ops->exit_hctx(hctx, i);
1433 q->mq_ops->free_hctx(hctx, i);
1436 free_percpu(q->queue_ctx);
1437 kfree(q->queue_hw_ctx);
1440 q->queue_ctx = NULL;
1441 q->queue_hw_ctx = NULL;
1444 mutex_lock(&all_q_mutex);
1445 list_del_init(&q->all_q_node);
1446 mutex_unlock(&all_q_mutex);
1448 EXPORT_SYMBOL(blk_mq_free_queue);
1450 /* Basically redo blk_mq_init_queue with queue frozen */
1451 static void blk_mq_queue_reinit(struct request_queue *q)
1453 blk_mq_freeze_queue(q);
1455 blk_mq_update_queue_map(q->mq_map, q->nr_hw_queues);
1458 * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
1459 * we should change hctx numa_node according to new topology (this
1460 * involves free and re-allocate memory, worthy doing?)
1463 blk_mq_map_swqueue(q);
1465 blk_mq_unfreeze_queue(q);
1468 static int blk_mq_queue_reinit_notify(struct notifier_block *nb,
1469 unsigned long action, void *hcpu)
1471 struct request_queue *q;
1474 * Before new mapping is established, hotadded cpu might already start
1475 * handling requests. This doesn't break anything as we map offline
1476 * CPUs to first hardware queue. We will re-init queue below to get
1479 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN &&
1480 action != CPU_ONLINE && action != CPU_ONLINE_FROZEN)
1483 mutex_lock(&all_q_mutex);
1484 list_for_each_entry(q, &all_q_list, all_q_node)
1485 blk_mq_queue_reinit(q);
1486 mutex_unlock(&all_q_mutex);
1490 static int __init blk_mq_init(void)
1494 for_each_possible_cpu(i)
1495 init_llist_head(&per_cpu(ipi_lists, i));
1499 /* Must be called after percpu_counter_hotcpu_callback() */
1500 hotcpu_notifier(blk_mq_queue_reinit_notify, -10);
1504 subsys_initcall(blk_mq_init);