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_rq(struct blk_mq_hw_ctx *hctx, gfp_t gfp,
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(struct blk_mq_hw_ctx *hctx,
197 gfp_t gfp, bool reserved)
199 return blk_mq_alloc_rq(hctx, gfp, reserved);
202 static struct request *blk_mq_alloc_request_pinned(struct request_queue *q,
209 struct blk_mq_ctx *ctx = blk_mq_get_ctx(q);
210 struct blk_mq_hw_ctx *hctx = q->mq_ops->map_queue(q, ctx->cpu);
212 rq = __blk_mq_alloc_request(hctx, gfp & ~__GFP_WAIT, reserved);
214 blk_mq_rq_ctx_init(q, ctx, rq, rw);
219 if (!(gfp & __GFP_WAIT))
222 __blk_mq_run_hw_queue(hctx);
223 blk_mq_wait_for_tags(hctx->tags);
229 struct request *blk_mq_alloc_request(struct request_queue *q, int rw, gfp_t gfp)
233 if (blk_mq_queue_enter(q))
236 rq = blk_mq_alloc_request_pinned(q, rw, gfp, false);
238 blk_mq_put_ctx(rq->mq_ctx);
242 struct request *blk_mq_alloc_reserved_request(struct request_queue *q, int rw,
247 if (blk_mq_queue_enter(q))
250 rq = blk_mq_alloc_request_pinned(q, rw, gfp, true);
252 blk_mq_put_ctx(rq->mq_ctx);
255 EXPORT_SYMBOL(blk_mq_alloc_reserved_request);
258 * Re-init and set pdu, if we have it
260 void blk_mq_rq_init(struct blk_mq_hw_ctx *hctx, struct request *rq)
262 blk_rq_init(hctx->queue, rq);
265 rq->special = blk_mq_rq_to_pdu(rq);
268 static void __blk_mq_free_request(struct blk_mq_hw_ctx *hctx,
269 struct blk_mq_ctx *ctx, struct request *rq)
271 const int tag = rq->tag;
272 struct request_queue *q = rq->q;
274 blk_mq_rq_init(hctx, rq);
275 blk_mq_put_tag(hctx->tags, tag);
277 blk_mq_queue_exit(q);
280 void blk_mq_free_request(struct request *rq)
282 struct blk_mq_ctx *ctx = rq->mq_ctx;
283 struct blk_mq_hw_ctx *hctx;
284 struct request_queue *q = rq->q;
286 ctx->rq_completed[rq_is_sync(rq)]++;
288 hctx = q->mq_ops->map_queue(q, ctx->cpu);
289 __blk_mq_free_request(hctx, ctx, rq);
292 static void blk_mq_bio_endio(struct request *rq, struct bio *bio, int error)
295 clear_bit(BIO_UPTODATE, &bio->bi_flags);
296 else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
299 if (unlikely(rq->cmd_flags & REQ_QUIET))
300 set_bit(BIO_QUIET, &bio->bi_flags);
302 /* don't actually finish bio if it's part of flush sequence */
303 if (!(rq->cmd_flags & REQ_FLUSH_SEQ))
304 bio_endio(bio, error);
307 void blk_mq_end_io(struct request *rq, int error)
309 struct bio *bio = rq->bio;
310 unsigned int bytes = 0;
312 trace_block_rq_complete(rq->q, rq);
315 struct bio *next = bio->bi_next;
318 bytes += bio->bi_iter.bi_size;
319 blk_mq_bio_endio(rq, bio, error);
323 blk_account_io_completion(rq, bytes);
325 blk_account_io_done(rq);
328 rq->end_io(rq, error);
330 blk_mq_free_request(rq);
332 EXPORT_SYMBOL(blk_mq_end_io);
334 static void __blk_mq_complete_request_remote(void *data)
336 struct request *rq = data;
338 rq->q->softirq_done_fn(rq);
341 void __blk_mq_complete_request(struct request *rq)
343 struct blk_mq_ctx *ctx = rq->mq_ctx;
346 if (!ctx->ipi_redirect) {
347 rq->q->softirq_done_fn(rq);
352 if (cpu != ctx->cpu && cpu_online(ctx->cpu)) {
353 rq->csd.func = __blk_mq_complete_request_remote;
356 __smp_call_function_single(ctx->cpu, &rq->csd, 0);
358 rq->q->softirq_done_fn(rq);
364 * blk_mq_complete_request - end I/O on a request
365 * @rq: the request being processed
368 * Ends all I/O on a request. It does not handle partial completions.
369 * The actual completion happens out-of-order, through a IPI handler.
371 void blk_mq_complete_request(struct request *rq)
373 if (unlikely(blk_should_fake_timeout(rq->q)))
375 if (!blk_mark_rq_complete(rq))
376 __blk_mq_complete_request(rq);
378 EXPORT_SYMBOL(blk_mq_complete_request);
380 static void blk_mq_start_request(struct request *rq)
382 struct request_queue *q = rq->q;
384 trace_block_rq_issue(q, rq);
387 * Just mark start time and set the started bit. Due to memory
388 * ordering, we know we'll see the correct deadline as long as
389 * REQ_ATOMIC_STARTED is seen.
391 rq->deadline = jiffies + q->rq_timeout;
392 set_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
395 static void blk_mq_requeue_request(struct request *rq)
397 struct request_queue *q = rq->q;
399 trace_block_rq_requeue(q, rq);
400 clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
403 struct blk_mq_timeout_data {
404 struct blk_mq_hw_ctx *hctx;
406 unsigned int *next_set;
409 static void blk_mq_timeout_check(void *__data, unsigned long *free_tags)
411 struct blk_mq_timeout_data *data = __data;
412 struct blk_mq_hw_ctx *hctx = data->hctx;
415 /* It may not be in flight yet (this is where
416 * the REQ_ATOMIC_STARTED flag comes in). The requests are
417 * statically allocated, so we know it's always safe to access the
418 * memory associated with a bit offset into ->rqs[].
424 tag = find_next_zero_bit(free_tags, hctx->queue_depth, tag);
425 if (tag >= hctx->queue_depth)
428 rq = hctx->rqs[tag++];
430 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
433 blk_rq_check_expired(rq, data->next, data->next_set);
437 static void blk_mq_hw_ctx_check_timeout(struct blk_mq_hw_ctx *hctx,
439 unsigned int *next_set)
441 struct blk_mq_timeout_data data = {
444 .next_set = next_set,
448 * Ask the tagging code to iterate busy requests, so we can
449 * check them for timeout.
451 blk_mq_tag_busy_iter(hctx->tags, blk_mq_timeout_check, &data);
454 static void blk_mq_rq_timer(unsigned long data)
456 struct request_queue *q = (struct request_queue *) data;
457 struct blk_mq_hw_ctx *hctx;
458 unsigned long next = 0;
461 queue_for_each_hw_ctx(q, hctx, i)
462 blk_mq_hw_ctx_check_timeout(hctx, &next, &next_set);
465 mod_timer(&q->timeout, round_jiffies_up(next));
469 * Reverse check our software queue for entries that we could potentially
470 * merge with. Currently includes a hand-wavy stop count of 8, to not spend
471 * too much time checking for merges.
473 static bool blk_mq_attempt_merge(struct request_queue *q,
474 struct blk_mq_ctx *ctx, struct bio *bio)
479 list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) {
485 if (!blk_rq_merge_ok(rq, bio))
488 el_ret = blk_try_merge(rq, bio);
489 if (el_ret == ELEVATOR_BACK_MERGE) {
490 if (bio_attempt_back_merge(q, rq, bio)) {
495 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
496 if (bio_attempt_front_merge(q, rq, bio)) {
507 void blk_mq_add_timer(struct request *rq)
509 __blk_add_timer(rq, NULL);
513 * Run this hardware queue, pulling any software queues mapped to it in.
514 * Note that this function currently has various problems around ordering
515 * of IO. In particular, we'd like FIFO behaviour on handling existing
516 * items on the hctx->dispatch list. Ignore that for now.
518 static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
520 struct request_queue *q = hctx->queue;
521 struct blk_mq_ctx *ctx;
526 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->flags)))
532 * Touch any software queue that has pending entries.
534 for_each_set_bit(bit, hctx->ctx_map, hctx->nr_ctx) {
535 clear_bit(bit, hctx->ctx_map);
536 ctx = hctx->ctxs[bit];
537 BUG_ON(bit != ctx->index_hw);
539 spin_lock(&ctx->lock);
540 list_splice_tail_init(&ctx->rq_list, &rq_list);
541 spin_unlock(&ctx->lock);
545 * If we have previous entries on our dispatch list, grab them
546 * and stuff them at the front for more fair dispatch.
548 if (!list_empty_careful(&hctx->dispatch)) {
549 spin_lock(&hctx->lock);
550 if (!list_empty(&hctx->dispatch))
551 list_splice_init(&hctx->dispatch, &rq_list);
552 spin_unlock(&hctx->lock);
556 * Delete and return all entries from our dispatch list
561 * Now process all the entries, sending them to the driver.
563 while (!list_empty(&rq_list)) {
566 rq = list_first_entry(&rq_list, struct request, queuelist);
567 list_del_init(&rq->queuelist);
568 blk_mq_start_request(rq);
570 if (q->dma_drain_size && blk_rq_bytes(rq)) {
572 * make sure space for the drain appears we
573 * know we can do this because max_hw_segments
574 * has been adjusted to be one fewer than the
577 rq->nr_phys_segments++;
581 * Last request in the series. Flag it as such, this
582 * enables drivers to know when IO should be kicked off,
583 * if they don't do it on a per-request basis.
585 * Note: the flag isn't the only condition drivers
586 * should do kick off. If drive is busy, the last
587 * request might not have the bit set.
589 if (list_empty(&rq_list))
590 rq->cmd_flags |= REQ_END;
592 ret = q->mq_ops->queue_rq(hctx, rq);
594 case BLK_MQ_RQ_QUEUE_OK:
597 case BLK_MQ_RQ_QUEUE_BUSY:
599 * FIXME: we should have a mechanism to stop the queue
600 * like blk_stop_queue, otherwise we will waste cpu
603 list_add(&rq->queuelist, &rq_list);
604 blk_mq_requeue_request(rq);
607 pr_err("blk-mq: bad return on queue: %d\n", ret);
608 case BLK_MQ_RQ_QUEUE_ERROR:
610 blk_mq_end_io(rq, rq->errors);
614 if (ret == BLK_MQ_RQ_QUEUE_BUSY)
619 hctx->dispatched[0]++;
620 else if (queued < (1 << (BLK_MQ_MAX_DISPATCH_ORDER - 1)))
621 hctx->dispatched[ilog2(queued) + 1]++;
624 * Any items that need requeuing? Stuff them into hctx->dispatch,
625 * that is where we will continue on next queue run.
627 if (!list_empty(&rq_list)) {
628 spin_lock(&hctx->lock);
629 list_splice(&rq_list, &hctx->dispatch);
630 spin_unlock(&hctx->lock);
634 void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
636 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->flags)))
640 __blk_mq_run_hw_queue(hctx);
642 struct request_queue *q = hctx->queue;
644 kblockd_schedule_delayed_work(q, &hctx->delayed_work, 0);
648 void blk_mq_run_queues(struct request_queue *q, bool async)
650 struct blk_mq_hw_ctx *hctx;
653 queue_for_each_hw_ctx(q, hctx, i) {
654 if ((!blk_mq_hctx_has_pending(hctx) &&
655 list_empty_careful(&hctx->dispatch)) ||
656 test_bit(BLK_MQ_S_STOPPED, &hctx->flags))
659 blk_mq_run_hw_queue(hctx, async);
662 EXPORT_SYMBOL(blk_mq_run_queues);
664 void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
666 cancel_delayed_work(&hctx->delayed_work);
667 set_bit(BLK_MQ_S_STOPPED, &hctx->state);
669 EXPORT_SYMBOL(blk_mq_stop_hw_queue);
671 void blk_mq_stop_hw_queues(struct request_queue *q)
673 struct blk_mq_hw_ctx *hctx;
676 queue_for_each_hw_ctx(q, hctx, i)
677 blk_mq_stop_hw_queue(hctx);
679 EXPORT_SYMBOL(blk_mq_stop_hw_queues);
681 void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
683 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
684 __blk_mq_run_hw_queue(hctx);
686 EXPORT_SYMBOL(blk_mq_start_hw_queue);
688 void blk_mq_start_stopped_hw_queues(struct request_queue *q)
690 struct blk_mq_hw_ctx *hctx;
693 queue_for_each_hw_ctx(q, hctx, i) {
694 if (!test_bit(BLK_MQ_S_STOPPED, &hctx->state))
697 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
698 blk_mq_run_hw_queue(hctx, true);
701 EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
703 static void blk_mq_work_fn(struct work_struct *work)
705 struct blk_mq_hw_ctx *hctx;
707 hctx = container_of(work, struct blk_mq_hw_ctx, delayed_work.work);
708 __blk_mq_run_hw_queue(hctx);
711 static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx,
712 struct request *rq, bool at_head)
714 struct blk_mq_ctx *ctx = rq->mq_ctx;
716 trace_block_rq_insert(hctx->queue, rq);
719 list_add(&rq->queuelist, &ctx->rq_list);
721 list_add_tail(&rq->queuelist, &ctx->rq_list);
722 blk_mq_hctx_mark_pending(hctx, ctx);
725 * We do this early, to ensure we are on the right CPU.
727 blk_mq_add_timer(rq);
730 void blk_mq_insert_request(struct request_queue *q, struct request *rq,
731 bool at_head, bool run_queue)
733 struct blk_mq_hw_ctx *hctx;
734 struct blk_mq_ctx *ctx, *current_ctx;
737 hctx = q->mq_ops->map_queue(q, ctx->cpu);
739 if (rq->cmd_flags & (REQ_FLUSH | REQ_FUA)) {
740 blk_insert_flush(rq);
742 current_ctx = blk_mq_get_ctx(q);
744 if (!cpu_online(ctx->cpu)) {
746 hctx = q->mq_ops->map_queue(q, ctx->cpu);
749 spin_lock(&ctx->lock);
750 __blk_mq_insert_request(hctx, rq, at_head);
751 spin_unlock(&ctx->lock);
753 blk_mq_put_ctx(current_ctx);
757 __blk_mq_run_hw_queue(hctx);
759 EXPORT_SYMBOL(blk_mq_insert_request);
762 * This is a special version of blk_mq_insert_request to bypass FLUSH request
763 * check. Should only be used internally.
765 void blk_mq_run_request(struct request *rq, bool run_queue, bool async)
767 struct request_queue *q = rq->q;
768 struct blk_mq_hw_ctx *hctx;
769 struct blk_mq_ctx *ctx, *current_ctx;
771 current_ctx = blk_mq_get_ctx(q);
774 if (!cpu_online(ctx->cpu)) {
778 hctx = q->mq_ops->map_queue(q, ctx->cpu);
780 /* ctx->cpu might be offline */
781 spin_lock(&ctx->lock);
782 __blk_mq_insert_request(hctx, rq, false);
783 spin_unlock(&ctx->lock);
785 blk_mq_put_ctx(current_ctx);
788 blk_mq_run_hw_queue(hctx, async);
791 static void blk_mq_insert_requests(struct request_queue *q,
792 struct blk_mq_ctx *ctx,
793 struct list_head *list,
798 struct blk_mq_hw_ctx *hctx;
799 struct blk_mq_ctx *current_ctx;
801 trace_block_unplug(q, depth, !from_schedule);
803 current_ctx = blk_mq_get_ctx(q);
805 if (!cpu_online(ctx->cpu))
807 hctx = q->mq_ops->map_queue(q, ctx->cpu);
810 * preemption doesn't flush plug list, so it's possible ctx->cpu is
813 spin_lock(&ctx->lock);
814 while (!list_empty(list)) {
817 rq = list_first_entry(list, struct request, queuelist);
818 list_del_init(&rq->queuelist);
820 __blk_mq_insert_request(hctx, rq, false);
822 spin_unlock(&ctx->lock);
824 blk_mq_put_ctx(current_ctx);
826 blk_mq_run_hw_queue(hctx, from_schedule);
829 static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b)
831 struct request *rqa = container_of(a, struct request, queuelist);
832 struct request *rqb = container_of(b, struct request, queuelist);
834 return !(rqa->mq_ctx < rqb->mq_ctx ||
835 (rqa->mq_ctx == rqb->mq_ctx &&
836 blk_rq_pos(rqa) < blk_rq_pos(rqb)));
839 void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
841 struct blk_mq_ctx *this_ctx;
842 struct request_queue *this_q;
848 list_splice_init(&plug->mq_list, &list);
850 list_sort(NULL, &list, plug_ctx_cmp);
856 while (!list_empty(&list)) {
857 rq = list_entry_rq(list.next);
858 list_del_init(&rq->queuelist);
860 if (rq->mq_ctx != this_ctx) {
862 blk_mq_insert_requests(this_q, this_ctx,
867 this_ctx = rq->mq_ctx;
873 list_add_tail(&rq->queuelist, &ctx_list);
877 * If 'this_ctx' is set, we know we have entries to complete
878 * on 'ctx_list'. Do those.
881 blk_mq_insert_requests(this_q, this_ctx, &ctx_list, depth,
886 static void blk_mq_bio_to_request(struct request *rq, struct bio *bio)
888 init_request_from_bio(rq, bio);
889 blk_account_io_start(rq, 1);
892 static void blk_mq_make_request(struct request_queue *q, struct bio *bio)
894 struct blk_mq_hw_ctx *hctx;
895 struct blk_mq_ctx *ctx;
896 const int is_sync = rw_is_sync(bio->bi_rw);
897 const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
898 int rw = bio_data_dir(bio);
900 unsigned int use_plug, request_count = 0;
903 * If we have multiple hardware queues, just go directly to
904 * one of those for sync IO.
906 use_plug = !is_flush_fua && ((q->nr_hw_queues == 1) || !is_sync);
908 blk_queue_bounce(q, &bio);
910 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
911 bio_endio(bio, -EIO);
915 if (use_plug && blk_attempt_plug_merge(q, bio, &request_count))
918 if (blk_mq_queue_enter(q)) {
919 bio_endio(bio, -EIO);
923 ctx = blk_mq_get_ctx(q);
924 hctx = q->mq_ops->map_queue(q, ctx->cpu);
926 trace_block_getrq(q, bio, rw);
927 rq = __blk_mq_alloc_request(hctx, GFP_ATOMIC, false);
929 blk_mq_rq_ctx_init(q, ctx, rq, rw);
932 trace_block_sleeprq(q, bio, rw);
933 rq = blk_mq_alloc_request_pinned(q, rw, __GFP_WAIT|GFP_ATOMIC,
936 hctx = q->mq_ops->map_queue(q, ctx->cpu);
941 if (unlikely(is_flush_fua)) {
942 blk_mq_bio_to_request(rq, bio);
944 blk_insert_flush(rq);
949 * A task plug currently exists. Since this is completely lockless,
950 * utilize that to temporarily store requests until the task is
951 * either done or scheduled away.
954 struct blk_plug *plug = current->plug;
957 blk_mq_bio_to_request(rq, bio);
958 if (list_empty(&plug->mq_list))
960 else if (request_count >= BLK_MAX_REQUEST_COUNT) {
961 blk_flush_plug_list(plug, false);
964 list_add_tail(&rq->queuelist, &plug->mq_list);
970 spin_lock(&ctx->lock);
972 if ((hctx->flags & BLK_MQ_F_SHOULD_MERGE) &&
973 blk_mq_attempt_merge(q, ctx, bio))
974 __blk_mq_free_request(hctx, ctx, rq);
976 blk_mq_bio_to_request(rq, bio);
977 __blk_mq_insert_request(hctx, rq, false);
980 spin_unlock(&ctx->lock);
984 * For a SYNC request, send it to the hardware immediately. For an
985 * ASYNC request, just ensure that we run it later on. The latter
986 * allows for merging opportunities and more efficient dispatching.
989 blk_mq_run_hw_queue(hctx, !is_sync || is_flush_fua);
993 * Default mapping to a software queue, since we use one per CPU.
995 struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q, const int cpu)
997 return q->queue_hw_ctx[q->mq_map[cpu]];
999 EXPORT_SYMBOL(blk_mq_map_queue);
1001 struct blk_mq_hw_ctx *blk_mq_alloc_single_hw_queue(struct blk_mq_reg *reg,
1002 unsigned int hctx_index)
1004 return kmalloc_node(sizeof(struct blk_mq_hw_ctx),
1005 GFP_KERNEL | __GFP_ZERO, reg->numa_node);
1007 EXPORT_SYMBOL(blk_mq_alloc_single_hw_queue);
1009 void blk_mq_free_single_hw_queue(struct blk_mq_hw_ctx *hctx,
1010 unsigned int hctx_index)
1014 EXPORT_SYMBOL(blk_mq_free_single_hw_queue);
1016 static void blk_mq_hctx_notify(void *data, unsigned long action,
1019 struct blk_mq_hw_ctx *hctx = data;
1020 struct blk_mq_ctx *ctx;
1023 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
1027 * Move ctx entries to new CPU, if this one is going away.
1029 ctx = __blk_mq_get_ctx(hctx->queue, cpu);
1031 spin_lock(&ctx->lock);
1032 if (!list_empty(&ctx->rq_list)) {
1033 list_splice_init(&ctx->rq_list, &tmp);
1034 clear_bit(ctx->index_hw, hctx->ctx_map);
1036 spin_unlock(&ctx->lock);
1038 if (list_empty(&tmp))
1041 ctx = blk_mq_get_ctx(hctx->queue);
1042 spin_lock(&ctx->lock);
1044 while (!list_empty(&tmp)) {
1047 rq = list_first_entry(&tmp, struct request, queuelist);
1049 list_move_tail(&rq->queuelist, &ctx->rq_list);
1052 blk_mq_hctx_mark_pending(hctx, ctx);
1054 spin_unlock(&ctx->lock);
1055 blk_mq_put_ctx(ctx);
1058 static void blk_mq_init_hw_commands(struct blk_mq_hw_ctx *hctx,
1059 void (*init)(void *, struct blk_mq_hw_ctx *,
1060 struct request *, unsigned int),
1065 for (i = 0; i < hctx->queue_depth; i++) {
1066 struct request *rq = hctx->rqs[i];
1068 init(data, hctx, rq, i);
1072 void blk_mq_init_commands(struct request_queue *q,
1073 void (*init)(void *, struct blk_mq_hw_ctx *,
1074 struct request *, unsigned int),
1077 struct blk_mq_hw_ctx *hctx;
1080 queue_for_each_hw_ctx(q, hctx, i)
1081 blk_mq_init_hw_commands(hctx, init, data);
1083 EXPORT_SYMBOL(blk_mq_init_commands);
1085 static void blk_mq_free_rq_map(struct blk_mq_hw_ctx *hctx)
1089 while (!list_empty(&hctx->page_list)) {
1090 page = list_first_entry(&hctx->page_list, struct page, lru);
1091 list_del_init(&page->lru);
1092 __free_pages(page, page->private);
1098 blk_mq_free_tags(hctx->tags);
1101 static size_t order_to_size(unsigned int order)
1103 size_t ret = PAGE_SIZE;
1111 static int blk_mq_init_rq_map(struct blk_mq_hw_ctx *hctx,
1112 unsigned int reserved_tags, int node)
1114 unsigned int i, j, entries_per_page, max_order = 4;
1115 size_t rq_size, left;
1117 INIT_LIST_HEAD(&hctx->page_list);
1119 hctx->rqs = kmalloc_node(hctx->queue_depth * sizeof(struct request *),
1125 * rq_size is the size of the request plus driver payload, rounded
1126 * to the cacheline size
1128 rq_size = round_up(sizeof(struct request) + hctx->cmd_size,
1130 left = rq_size * hctx->queue_depth;
1132 for (i = 0; i < hctx->queue_depth;) {
1133 int this_order = max_order;
1138 while (left < order_to_size(this_order - 1) && this_order)
1142 page = alloc_pages_node(node, GFP_KERNEL, this_order);
1147 if (order_to_size(this_order) < rq_size)
1154 page->private = this_order;
1155 list_add_tail(&page->lru, &hctx->page_list);
1157 p = page_address(page);
1158 entries_per_page = order_to_size(this_order) / rq_size;
1159 to_do = min(entries_per_page, hctx->queue_depth - i);
1160 left -= to_do * rq_size;
1161 for (j = 0; j < to_do; j++) {
1163 blk_mq_rq_init(hctx, hctx->rqs[i]);
1169 if (i < (reserved_tags + BLK_MQ_TAG_MIN))
1171 else if (i != hctx->queue_depth) {
1172 hctx->queue_depth = i;
1173 pr_warn("%s: queue depth set to %u because of low memory\n",
1177 hctx->tags = blk_mq_init_tags(hctx->queue_depth, reserved_tags, node);
1180 blk_mq_free_rq_map(hctx);
1187 static int blk_mq_init_hw_queues(struct request_queue *q,
1188 struct blk_mq_reg *reg, void *driver_data)
1190 struct blk_mq_hw_ctx *hctx;
1194 * Initialize hardware queues
1196 queue_for_each_hw_ctx(q, hctx, i) {
1197 unsigned int num_maps;
1200 node = hctx->numa_node;
1201 if (node == NUMA_NO_NODE)
1202 node = hctx->numa_node = reg->numa_node;
1204 INIT_DELAYED_WORK(&hctx->delayed_work, blk_mq_work_fn);
1205 spin_lock_init(&hctx->lock);
1206 INIT_LIST_HEAD(&hctx->dispatch);
1208 hctx->queue_num = i;
1209 hctx->flags = reg->flags;
1210 hctx->queue_depth = reg->queue_depth;
1211 hctx->cmd_size = reg->cmd_size;
1213 blk_mq_init_cpu_notifier(&hctx->cpu_notifier,
1214 blk_mq_hctx_notify, hctx);
1215 blk_mq_register_cpu_notifier(&hctx->cpu_notifier);
1217 if (blk_mq_init_rq_map(hctx, reg->reserved_tags, node))
1221 * Allocate space for all possible cpus to avoid allocation in
1224 hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *),
1229 num_maps = ALIGN(nr_cpu_ids, BITS_PER_LONG) / BITS_PER_LONG;
1230 hctx->ctx_map = kzalloc_node(num_maps * sizeof(unsigned long),
1235 hctx->nr_ctx_map = num_maps;
1238 if (reg->ops->init_hctx &&
1239 reg->ops->init_hctx(hctx, driver_data, i))
1243 if (i == q->nr_hw_queues)
1249 queue_for_each_hw_ctx(q, hctx, j) {
1253 if (reg->ops->exit_hctx)
1254 reg->ops->exit_hctx(hctx, j);
1256 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
1257 blk_mq_free_rq_map(hctx);
1264 static void blk_mq_init_cpu_queues(struct request_queue *q,
1265 unsigned int nr_hw_queues)
1269 for_each_possible_cpu(i) {
1270 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
1271 struct blk_mq_hw_ctx *hctx;
1273 memset(__ctx, 0, sizeof(*__ctx));
1275 spin_lock_init(&__ctx->lock);
1276 INIT_LIST_HEAD(&__ctx->rq_list);
1279 /* If the cpu isn't online, the cpu is mapped to first hctx */
1280 hctx = q->mq_ops->map_queue(q, i);
1287 * Set local node, IFF we have more than one hw queue. If
1288 * not, we remain on the home node of the device
1290 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
1291 hctx->numa_node = cpu_to_node(i);
1295 static void blk_mq_map_swqueue(struct request_queue *q)
1298 struct blk_mq_hw_ctx *hctx;
1299 struct blk_mq_ctx *ctx;
1301 queue_for_each_hw_ctx(q, hctx, i) {
1306 * Map software to hardware queues
1308 queue_for_each_ctx(q, ctx, i) {
1309 /* If the cpu isn't online, the cpu is mapped to first hctx */
1310 hctx = q->mq_ops->map_queue(q, i);
1311 ctx->index_hw = hctx->nr_ctx;
1312 hctx->ctxs[hctx->nr_ctx++] = ctx;
1316 struct request_queue *blk_mq_init_queue(struct blk_mq_reg *reg,
1319 struct blk_mq_hw_ctx **hctxs;
1320 struct blk_mq_ctx *ctx;
1321 struct request_queue *q;
1324 if (!reg->nr_hw_queues ||
1325 !reg->ops->queue_rq || !reg->ops->map_queue ||
1326 !reg->ops->alloc_hctx || !reg->ops->free_hctx)
1327 return ERR_PTR(-EINVAL);
1329 if (!reg->queue_depth)
1330 reg->queue_depth = BLK_MQ_MAX_DEPTH;
1331 else if (reg->queue_depth > BLK_MQ_MAX_DEPTH) {
1332 pr_err("blk-mq: queuedepth too large (%u)\n", reg->queue_depth);
1333 reg->queue_depth = BLK_MQ_MAX_DEPTH;
1336 if (reg->queue_depth < (reg->reserved_tags + BLK_MQ_TAG_MIN))
1337 return ERR_PTR(-EINVAL);
1339 ctx = alloc_percpu(struct blk_mq_ctx);
1341 return ERR_PTR(-ENOMEM);
1343 hctxs = kmalloc_node(reg->nr_hw_queues * sizeof(*hctxs), GFP_KERNEL,
1349 for (i = 0; i < reg->nr_hw_queues; i++) {
1350 hctxs[i] = reg->ops->alloc_hctx(reg, i);
1354 hctxs[i]->numa_node = NUMA_NO_NODE;
1355 hctxs[i]->queue_num = i;
1358 q = blk_alloc_queue_node(GFP_KERNEL, reg->numa_node);
1362 q->mq_map = blk_mq_make_queue_map(reg);
1366 setup_timer(&q->timeout, blk_mq_rq_timer, (unsigned long) q);
1367 blk_queue_rq_timeout(q, 30000);
1369 q->nr_queues = nr_cpu_ids;
1370 q->nr_hw_queues = reg->nr_hw_queues;
1373 q->queue_hw_ctx = hctxs;
1375 q->mq_ops = reg->ops;
1376 q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
1378 q->sg_reserved_size = INT_MAX;
1380 blk_queue_make_request(q, blk_mq_make_request);
1381 blk_queue_rq_timed_out(q, reg->ops->timeout);
1383 blk_queue_rq_timeout(q, reg->timeout);
1385 if (reg->ops->complete)
1386 blk_queue_softirq_done(q, reg->ops->complete);
1388 blk_mq_init_flush(q);
1389 blk_mq_init_cpu_queues(q, reg->nr_hw_queues);
1391 q->flush_rq = kzalloc(round_up(sizeof(struct request) + reg->cmd_size,
1392 cache_line_size()), GFP_KERNEL);
1396 if (blk_mq_init_hw_queues(q, reg, driver_data))
1399 blk_mq_map_swqueue(q);
1401 mutex_lock(&all_q_mutex);
1402 list_add_tail(&q->all_q_node, &all_q_list);
1403 mutex_unlock(&all_q_mutex);
1412 blk_cleanup_queue(q);
1414 for (i = 0; i < reg->nr_hw_queues; i++) {
1417 reg->ops->free_hctx(hctxs[i], i);
1422 return ERR_PTR(-ENOMEM);
1424 EXPORT_SYMBOL(blk_mq_init_queue);
1426 void blk_mq_free_queue(struct request_queue *q)
1428 struct blk_mq_hw_ctx *hctx;
1431 queue_for_each_hw_ctx(q, hctx, i) {
1432 kfree(hctx->ctx_map);
1434 blk_mq_free_rq_map(hctx);
1435 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
1436 if (q->mq_ops->exit_hctx)
1437 q->mq_ops->exit_hctx(hctx, i);
1438 q->mq_ops->free_hctx(hctx, i);
1441 free_percpu(q->queue_ctx);
1442 kfree(q->queue_hw_ctx);
1445 q->queue_ctx = NULL;
1446 q->queue_hw_ctx = NULL;
1449 mutex_lock(&all_q_mutex);
1450 list_del_init(&q->all_q_node);
1451 mutex_unlock(&all_q_mutex);
1454 /* Basically redo blk_mq_init_queue with queue frozen */
1455 static void blk_mq_queue_reinit(struct request_queue *q)
1457 blk_mq_freeze_queue(q);
1459 blk_mq_update_queue_map(q->mq_map, q->nr_hw_queues);
1462 * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
1463 * we should change hctx numa_node according to new topology (this
1464 * involves free and re-allocate memory, worthy doing?)
1467 blk_mq_map_swqueue(q);
1469 blk_mq_unfreeze_queue(q);
1472 static int blk_mq_queue_reinit_notify(struct notifier_block *nb,
1473 unsigned long action, void *hcpu)
1475 struct request_queue *q;
1478 * Before new mapping is established, hotadded cpu might already start
1479 * handling requests. This doesn't break anything as we map offline
1480 * CPUs to first hardware queue. We will re-init queue below to get
1483 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN &&
1484 action != CPU_ONLINE && action != CPU_ONLINE_FROZEN)
1487 mutex_lock(&all_q_mutex);
1488 list_for_each_entry(q, &all_q_list, all_q_node)
1489 blk_mq_queue_reinit(q);
1490 mutex_unlock(&all_q_mutex);
1494 static int __init blk_mq_init(void)
1498 /* Must be called after percpu_counter_hotcpu_callback() */
1499 hotcpu_notifier(blk_mq_queue_reinit_notify, -10);
1503 subsys_initcall(blk_mq_init);