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 bool blk_mq_end_io_partial(struct request *rq, int error, unsigned int nr_bytes)
288 if (blk_update_request(rq, error, blk_rq_bytes(rq)))
291 blk_account_io_done(rq);
294 rq->end_io(rq, error);
296 blk_mq_free_request(rq);
299 EXPORT_SYMBOL(blk_mq_end_io_partial);
301 static void __blk_mq_complete_request_remote(void *data)
303 struct request *rq = data;
305 rq->q->softirq_done_fn(rq);
308 void __blk_mq_complete_request(struct request *rq)
310 struct blk_mq_ctx *ctx = rq->mq_ctx;
313 if (!ctx->ipi_redirect) {
314 rq->q->softirq_done_fn(rq);
319 if (cpu != ctx->cpu && cpu_online(ctx->cpu)) {
320 rq->csd.func = __blk_mq_complete_request_remote;
323 __smp_call_function_single(ctx->cpu, &rq->csd, 0);
325 rq->q->softirq_done_fn(rq);
331 * blk_mq_complete_request - end I/O on a request
332 * @rq: the request being processed
335 * Ends all I/O on a request. It does not handle partial completions.
336 * The actual completion happens out-of-order, through a IPI handler.
338 void blk_mq_complete_request(struct request *rq)
340 if (unlikely(blk_should_fake_timeout(rq->q)))
342 if (!blk_mark_rq_complete(rq))
343 __blk_mq_complete_request(rq);
345 EXPORT_SYMBOL(blk_mq_complete_request);
347 static void blk_mq_start_request(struct request *rq, bool last)
349 struct request_queue *q = rq->q;
351 trace_block_rq_issue(q, rq);
354 * Just mark start time and set the started bit. Due to memory
355 * ordering, we know we'll see the correct deadline as long as
356 * REQ_ATOMIC_STARTED is seen.
358 rq->deadline = jiffies + q->rq_timeout;
359 set_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
361 if (q->dma_drain_size && blk_rq_bytes(rq)) {
363 * Make sure space for the drain appears. We know we can do
364 * this because max_hw_segments has been adjusted to be one
365 * fewer than the device can handle.
367 rq->nr_phys_segments++;
371 * Flag the last request in the series so that drivers know when IO
372 * should be kicked off, if they don't do it on a per-request basis.
374 * Note: the flag isn't the only condition drivers should do kick off.
375 * If drive is busy, the last request might not have the bit set.
378 rq->cmd_flags |= REQ_END;
381 static void blk_mq_requeue_request(struct request *rq)
383 struct request_queue *q = rq->q;
385 trace_block_rq_requeue(q, rq);
386 clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
388 rq->cmd_flags &= ~REQ_END;
390 if (q->dma_drain_size && blk_rq_bytes(rq))
391 rq->nr_phys_segments--;
394 struct blk_mq_timeout_data {
395 struct blk_mq_hw_ctx *hctx;
397 unsigned int *next_set;
400 static void blk_mq_timeout_check(void *__data, unsigned long *free_tags)
402 struct blk_mq_timeout_data *data = __data;
403 struct blk_mq_hw_ctx *hctx = data->hctx;
406 /* It may not be in flight yet (this is where
407 * the REQ_ATOMIC_STARTED flag comes in). The requests are
408 * statically allocated, so we know it's always safe to access the
409 * memory associated with a bit offset into ->rqs[].
415 tag = find_next_zero_bit(free_tags, hctx->queue_depth, tag);
416 if (tag >= hctx->queue_depth)
419 rq = hctx->rqs[tag++];
421 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
424 blk_rq_check_expired(rq, data->next, data->next_set);
428 static void blk_mq_hw_ctx_check_timeout(struct blk_mq_hw_ctx *hctx,
430 unsigned int *next_set)
432 struct blk_mq_timeout_data data = {
435 .next_set = next_set,
439 * Ask the tagging code to iterate busy requests, so we can
440 * check them for timeout.
442 blk_mq_tag_busy_iter(hctx->tags, blk_mq_timeout_check, &data);
445 static void blk_mq_rq_timer(unsigned long data)
447 struct request_queue *q = (struct request_queue *) data;
448 struct blk_mq_hw_ctx *hctx;
449 unsigned long next = 0;
452 queue_for_each_hw_ctx(q, hctx, i)
453 blk_mq_hw_ctx_check_timeout(hctx, &next, &next_set);
456 mod_timer(&q->timeout, round_jiffies_up(next));
460 * Reverse check our software queue for entries that we could potentially
461 * merge with. Currently includes a hand-wavy stop count of 8, to not spend
462 * too much time checking for merges.
464 static bool blk_mq_attempt_merge(struct request_queue *q,
465 struct blk_mq_ctx *ctx, struct bio *bio)
470 list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) {
476 if (!blk_rq_merge_ok(rq, bio))
479 el_ret = blk_try_merge(rq, bio);
480 if (el_ret == ELEVATOR_BACK_MERGE) {
481 if (bio_attempt_back_merge(q, rq, bio)) {
486 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
487 if (bio_attempt_front_merge(q, rq, bio)) {
498 void blk_mq_add_timer(struct request *rq)
500 __blk_add_timer(rq, NULL);
504 * Run this hardware queue, pulling any software queues mapped to it in.
505 * Note that this function currently has various problems around ordering
506 * of IO. In particular, we'd like FIFO behaviour on handling existing
507 * items on the hctx->dispatch list. Ignore that for now.
509 static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
511 struct request_queue *q = hctx->queue;
512 struct blk_mq_ctx *ctx;
517 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->flags)))
523 * Touch any software queue that has pending entries.
525 for_each_set_bit(bit, hctx->ctx_map, hctx->nr_ctx) {
526 clear_bit(bit, hctx->ctx_map);
527 ctx = hctx->ctxs[bit];
528 BUG_ON(bit != ctx->index_hw);
530 spin_lock(&ctx->lock);
531 list_splice_tail_init(&ctx->rq_list, &rq_list);
532 spin_unlock(&ctx->lock);
536 * If we have previous entries on our dispatch list, grab them
537 * and stuff them at the front for more fair dispatch.
539 if (!list_empty_careful(&hctx->dispatch)) {
540 spin_lock(&hctx->lock);
541 if (!list_empty(&hctx->dispatch))
542 list_splice_init(&hctx->dispatch, &rq_list);
543 spin_unlock(&hctx->lock);
547 * Delete and return all entries from our dispatch list
552 * Now process all the entries, sending them to the driver.
554 while (!list_empty(&rq_list)) {
557 rq = list_first_entry(&rq_list, struct request, queuelist);
558 list_del_init(&rq->queuelist);
560 blk_mq_start_request(rq, list_empty(&rq_list));
562 ret = q->mq_ops->queue_rq(hctx, rq);
564 case BLK_MQ_RQ_QUEUE_OK:
567 case BLK_MQ_RQ_QUEUE_BUSY:
569 * FIXME: we should have a mechanism to stop the queue
570 * like blk_stop_queue, otherwise we will waste cpu
573 list_add(&rq->queuelist, &rq_list);
574 blk_mq_requeue_request(rq);
577 pr_err("blk-mq: bad return on queue: %d\n", ret);
578 case BLK_MQ_RQ_QUEUE_ERROR:
580 blk_mq_end_io(rq, rq->errors);
584 if (ret == BLK_MQ_RQ_QUEUE_BUSY)
589 hctx->dispatched[0]++;
590 else if (queued < (1 << (BLK_MQ_MAX_DISPATCH_ORDER - 1)))
591 hctx->dispatched[ilog2(queued) + 1]++;
594 * Any items that need requeuing? Stuff them into hctx->dispatch,
595 * that is where we will continue on next queue run.
597 if (!list_empty(&rq_list)) {
598 spin_lock(&hctx->lock);
599 list_splice(&rq_list, &hctx->dispatch);
600 spin_unlock(&hctx->lock);
604 void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
606 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->flags)))
610 __blk_mq_run_hw_queue(hctx);
612 struct request_queue *q = hctx->queue;
614 kblockd_schedule_delayed_work(q, &hctx->delayed_work, 0);
618 void blk_mq_run_queues(struct request_queue *q, bool async)
620 struct blk_mq_hw_ctx *hctx;
623 queue_for_each_hw_ctx(q, hctx, i) {
624 if ((!blk_mq_hctx_has_pending(hctx) &&
625 list_empty_careful(&hctx->dispatch)) ||
626 test_bit(BLK_MQ_S_STOPPED, &hctx->flags))
629 blk_mq_run_hw_queue(hctx, async);
632 EXPORT_SYMBOL(blk_mq_run_queues);
634 void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
636 cancel_delayed_work(&hctx->delayed_work);
637 set_bit(BLK_MQ_S_STOPPED, &hctx->state);
639 EXPORT_SYMBOL(blk_mq_stop_hw_queue);
641 void blk_mq_stop_hw_queues(struct request_queue *q)
643 struct blk_mq_hw_ctx *hctx;
646 queue_for_each_hw_ctx(q, hctx, i)
647 blk_mq_stop_hw_queue(hctx);
649 EXPORT_SYMBOL(blk_mq_stop_hw_queues);
651 void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
653 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
654 __blk_mq_run_hw_queue(hctx);
656 EXPORT_SYMBOL(blk_mq_start_hw_queue);
658 void blk_mq_start_stopped_hw_queues(struct request_queue *q)
660 struct blk_mq_hw_ctx *hctx;
663 queue_for_each_hw_ctx(q, hctx, i) {
664 if (!test_bit(BLK_MQ_S_STOPPED, &hctx->state))
667 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
668 blk_mq_run_hw_queue(hctx, true);
671 EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
673 static void blk_mq_work_fn(struct work_struct *work)
675 struct blk_mq_hw_ctx *hctx;
677 hctx = container_of(work, struct blk_mq_hw_ctx, delayed_work.work);
678 __blk_mq_run_hw_queue(hctx);
681 static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx,
682 struct request *rq, bool at_head)
684 struct blk_mq_ctx *ctx = rq->mq_ctx;
686 trace_block_rq_insert(hctx->queue, rq);
689 list_add(&rq->queuelist, &ctx->rq_list);
691 list_add_tail(&rq->queuelist, &ctx->rq_list);
692 blk_mq_hctx_mark_pending(hctx, ctx);
695 * We do this early, to ensure we are on the right CPU.
697 blk_mq_add_timer(rq);
700 void blk_mq_insert_request(struct request *rq, bool at_head, bool run_queue,
703 struct request_queue *q = rq->q;
704 struct blk_mq_hw_ctx *hctx;
705 struct blk_mq_ctx *ctx = rq->mq_ctx, *current_ctx;
707 current_ctx = blk_mq_get_ctx(q);
708 if (!cpu_online(ctx->cpu))
709 rq->mq_ctx = ctx = current_ctx;
711 hctx = q->mq_ops->map_queue(q, ctx->cpu);
713 if (rq->cmd_flags & (REQ_FLUSH | REQ_FUA) &&
714 !(rq->cmd_flags & (REQ_FLUSH_SEQ))) {
715 blk_insert_flush(rq);
717 spin_lock(&ctx->lock);
718 __blk_mq_insert_request(hctx, rq, at_head);
719 spin_unlock(&ctx->lock);
722 blk_mq_put_ctx(current_ctx);
725 blk_mq_run_hw_queue(hctx, async);
728 static void blk_mq_insert_requests(struct request_queue *q,
729 struct blk_mq_ctx *ctx,
730 struct list_head *list,
735 struct blk_mq_hw_ctx *hctx;
736 struct blk_mq_ctx *current_ctx;
738 trace_block_unplug(q, depth, !from_schedule);
740 current_ctx = blk_mq_get_ctx(q);
742 if (!cpu_online(ctx->cpu))
744 hctx = q->mq_ops->map_queue(q, ctx->cpu);
747 * preemption doesn't flush plug list, so it's possible ctx->cpu is
750 spin_lock(&ctx->lock);
751 while (!list_empty(list)) {
754 rq = list_first_entry(list, struct request, queuelist);
755 list_del_init(&rq->queuelist);
757 __blk_mq_insert_request(hctx, rq, false);
759 spin_unlock(&ctx->lock);
761 blk_mq_put_ctx(current_ctx);
763 blk_mq_run_hw_queue(hctx, from_schedule);
766 static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b)
768 struct request *rqa = container_of(a, struct request, queuelist);
769 struct request *rqb = container_of(b, struct request, queuelist);
771 return !(rqa->mq_ctx < rqb->mq_ctx ||
772 (rqa->mq_ctx == rqb->mq_ctx &&
773 blk_rq_pos(rqa) < blk_rq_pos(rqb)));
776 void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
778 struct blk_mq_ctx *this_ctx;
779 struct request_queue *this_q;
785 list_splice_init(&plug->mq_list, &list);
787 list_sort(NULL, &list, plug_ctx_cmp);
793 while (!list_empty(&list)) {
794 rq = list_entry_rq(list.next);
795 list_del_init(&rq->queuelist);
797 if (rq->mq_ctx != this_ctx) {
799 blk_mq_insert_requests(this_q, this_ctx,
804 this_ctx = rq->mq_ctx;
810 list_add_tail(&rq->queuelist, &ctx_list);
814 * If 'this_ctx' is set, we know we have entries to complete
815 * on 'ctx_list'. Do those.
818 blk_mq_insert_requests(this_q, this_ctx, &ctx_list, depth,
823 static void blk_mq_bio_to_request(struct request *rq, struct bio *bio)
825 init_request_from_bio(rq, bio);
826 blk_account_io_start(rq, 1);
829 static void blk_mq_make_request(struct request_queue *q, struct bio *bio)
831 struct blk_mq_hw_ctx *hctx;
832 struct blk_mq_ctx *ctx;
833 const int is_sync = rw_is_sync(bio->bi_rw);
834 const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
835 int rw = bio_data_dir(bio);
837 unsigned int use_plug, request_count = 0;
840 * If we have multiple hardware queues, just go directly to
841 * one of those for sync IO.
843 use_plug = !is_flush_fua && ((q->nr_hw_queues == 1) || !is_sync);
845 blk_queue_bounce(q, &bio);
847 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
848 bio_endio(bio, -EIO);
852 if (use_plug && blk_attempt_plug_merge(q, bio, &request_count))
855 if (blk_mq_queue_enter(q)) {
856 bio_endio(bio, -EIO);
860 ctx = blk_mq_get_ctx(q);
861 hctx = q->mq_ops->map_queue(q, ctx->cpu);
865 trace_block_getrq(q, bio, rw);
866 rq = __blk_mq_alloc_request(hctx, GFP_ATOMIC, false);
868 blk_mq_rq_ctx_init(q, ctx, rq, rw);
871 trace_block_sleeprq(q, bio, rw);
872 rq = blk_mq_alloc_request_pinned(q, rw, __GFP_WAIT|GFP_ATOMIC,
875 hctx = q->mq_ops->map_queue(q, ctx->cpu);
880 if (unlikely(is_flush_fua)) {
881 blk_mq_bio_to_request(rq, bio);
883 blk_insert_flush(rq);
888 * A task plug currently exists. Since this is completely lockless,
889 * utilize that to temporarily store requests until the task is
890 * either done or scheduled away.
893 struct blk_plug *plug = current->plug;
896 blk_mq_bio_to_request(rq, bio);
897 if (list_empty(&plug->mq_list))
899 else if (request_count >= BLK_MAX_REQUEST_COUNT) {
900 blk_flush_plug_list(plug, false);
903 list_add_tail(&rq->queuelist, &plug->mq_list);
909 spin_lock(&ctx->lock);
911 if ((hctx->flags & BLK_MQ_F_SHOULD_MERGE) &&
912 blk_mq_attempt_merge(q, ctx, bio))
913 __blk_mq_free_request(hctx, ctx, rq);
915 blk_mq_bio_to_request(rq, bio);
916 __blk_mq_insert_request(hctx, rq, false);
919 spin_unlock(&ctx->lock);
923 * For a SYNC request, send it to the hardware immediately. For an
924 * ASYNC request, just ensure that we run it later on. The latter
925 * allows for merging opportunities and more efficient dispatching.
928 blk_mq_run_hw_queue(hctx, !is_sync || is_flush_fua);
932 * Default mapping to a software queue, since we use one per CPU.
934 struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q, const int cpu)
936 return q->queue_hw_ctx[q->mq_map[cpu]];
938 EXPORT_SYMBOL(blk_mq_map_queue);
940 struct blk_mq_hw_ctx *blk_mq_alloc_single_hw_queue(struct blk_mq_reg *reg,
941 unsigned int hctx_index)
943 return kmalloc_node(sizeof(struct blk_mq_hw_ctx),
944 GFP_KERNEL | __GFP_ZERO, reg->numa_node);
946 EXPORT_SYMBOL(blk_mq_alloc_single_hw_queue);
948 void blk_mq_free_single_hw_queue(struct blk_mq_hw_ctx *hctx,
949 unsigned int hctx_index)
953 EXPORT_SYMBOL(blk_mq_free_single_hw_queue);
955 static void blk_mq_hctx_notify(void *data, unsigned long action,
958 struct blk_mq_hw_ctx *hctx = data;
959 struct blk_mq_ctx *ctx;
962 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
966 * Move ctx entries to new CPU, if this one is going away.
968 ctx = __blk_mq_get_ctx(hctx->queue, cpu);
970 spin_lock(&ctx->lock);
971 if (!list_empty(&ctx->rq_list)) {
972 list_splice_init(&ctx->rq_list, &tmp);
973 clear_bit(ctx->index_hw, hctx->ctx_map);
975 spin_unlock(&ctx->lock);
977 if (list_empty(&tmp))
980 ctx = blk_mq_get_ctx(hctx->queue);
981 spin_lock(&ctx->lock);
983 while (!list_empty(&tmp)) {
986 rq = list_first_entry(&tmp, struct request, queuelist);
988 list_move_tail(&rq->queuelist, &ctx->rq_list);
991 blk_mq_hctx_mark_pending(hctx, ctx);
993 spin_unlock(&ctx->lock);
997 static void blk_mq_init_hw_commands(struct blk_mq_hw_ctx *hctx,
998 void (*init)(void *, struct blk_mq_hw_ctx *,
999 struct request *, unsigned int),
1004 for (i = 0; i < hctx->queue_depth; i++) {
1005 struct request *rq = hctx->rqs[i];
1007 init(data, hctx, rq, i);
1011 void blk_mq_init_commands(struct request_queue *q,
1012 void (*init)(void *, struct blk_mq_hw_ctx *,
1013 struct request *, unsigned int),
1016 struct blk_mq_hw_ctx *hctx;
1019 queue_for_each_hw_ctx(q, hctx, i)
1020 blk_mq_init_hw_commands(hctx, init, data);
1022 EXPORT_SYMBOL(blk_mq_init_commands);
1024 static void blk_mq_free_rq_map(struct blk_mq_hw_ctx *hctx)
1028 while (!list_empty(&hctx->page_list)) {
1029 page = list_first_entry(&hctx->page_list, struct page, lru);
1030 list_del_init(&page->lru);
1031 __free_pages(page, page->private);
1037 blk_mq_free_tags(hctx->tags);
1040 static size_t order_to_size(unsigned int order)
1042 size_t ret = PAGE_SIZE;
1050 static int blk_mq_init_rq_map(struct blk_mq_hw_ctx *hctx,
1051 unsigned int reserved_tags, int node)
1053 unsigned int i, j, entries_per_page, max_order = 4;
1054 size_t rq_size, left;
1056 INIT_LIST_HEAD(&hctx->page_list);
1058 hctx->rqs = kmalloc_node(hctx->queue_depth * sizeof(struct request *),
1064 * rq_size is the size of the request plus driver payload, rounded
1065 * to the cacheline size
1067 rq_size = round_up(sizeof(struct request) + hctx->cmd_size,
1069 left = rq_size * hctx->queue_depth;
1071 for (i = 0; i < hctx->queue_depth;) {
1072 int this_order = max_order;
1077 while (left < order_to_size(this_order - 1) && this_order)
1081 page = alloc_pages_node(node, GFP_KERNEL, this_order);
1086 if (order_to_size(this_order) < rq_size)
1093 page->private = this_order;
1094 list_add_tail(&page->lru, &hctx->page_list);
1096 p = page_address(page);
1097 entries_per_page = order_to_size(this_order) / rq_size;
1098 to_do = min(entries_per_page, hctx->queue_depth - i);
1099 left -= to_do * rq_size;
1100 for (j = 0; j < to_do; j++) {
1102 blk_mq_rq_init(hctx, hctx->rqs[i]);
1108 if (i < (reserved_tags + BLK_MQ_TAG_MIN))
1110 else if (i != hctx->queue_depth) {
1111 hctx->queue_depth = i;
1112 pr_warn("%s: queue depth set to %u because of low memory\n",
1116 hctx->tags = blk_mq_init_tags(hctx->queue_depth, reserved_tags, node);
1119 blk_mq_free_rq_map(hctx);
1126 static int blk_mq_init_hw_queues(struct request_queue *q,
1127 struct blk_mq_reg *reg, void *driver_data)
1129 struct blk_mq_hw_ctx *hctx;
1133 * Initialize hardware queues
1135 queue_for_each_hw_ctx(q, hctx, i) {
1136 unsigned int num_maps;
1139 node = hctx->numa_node;
1140 if (node == NUMA_NO_NODE)
1141 node = hctx->numa_node = reg->numa_node;
1143 INIT_DELAYED_WORK(&hctx->delayed_work, blk_mq_work_fn);
1144 spin_lock_init(&hctx->lock);
1145 INIT_LIST_HEAD(&hctx->dispatch);
1147 hctx->queue_num = i;
1148 hctx->flags = reg->flags;
1149 hctx->queue_depth = reg->queue_depth;
1150 hctx->cmd_size = reg->cmd_size;
1152 blk_mq_init_cpu_notifier(&hctx->cpu_notifier,
1153 blk_mq_hctx_notify, hctx);
1154 blk_mq_register_cpu_notifier(&hctx->cpu_notifier);
1156 if (blk_mq_init_rq_map(hctx, reg->reserved_tags, node))
1160 * Allocate space for all possible cpus to avoid allocation in
1163 hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *),
1168 num_maps = ALIGN(nr_cpu_ids, BITS_PER_LONG) / BITS_PER_LONG;
1169 hctx->ctx_map = kzalloc_node(num_maps * sizeof(unsigned long),
1174 hctx->nr_ctx_map = num_maps;
1177 if (reg->ops->init_hctx &&
1178 reg->ops->init_hctx(hctx, driver_data, i))
1182 if (i == q->nr_hw_queues)
1188 queue_for_each_hw_ctx(q, hctx, j) {
1192 if (reg->ops->exit_hctx)
1193 reg->ops->exit_hctx(hctx, j);
1195 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
1196 blk_mq_free_rq_map(hctx);
1203 static void blk_mq_init_cpu_queues(struct request_queue *q,
1204 unsigned int nr_hw_queues)
1208 for_each_possible_cpu(i) {
1209 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
1210 struct blk_mq_hw_ctx *hctx;
1212 memset(__ctx, 0, sizeof(*__ctx));
1214 spin_lock_init(&__ctx->lock);
1215 INIT_LIST_HEAD(&__ctx->rq_list);
1218 /* If the cpu isn't online, the cpu is mapped to first hctx */
1219 hctx = q->mq_ops->map_queue(q, i);
1226 * Set local node, IFF we have more than one hw queue. If
1227 * not, we remain on the home node of the device
1229 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
1230 hctx->numa_node = cpu_to_node(i);
1234 static void blk_mq_map_swqueue(struct request_queue *q)
1237 struct blk_mq_hw_ctx *hctx;
1238 struct blk_mq_ctx *ctx;
1240 queue_for_each_hw_ctx(q, hctx, i) {
1245 * Map software to hardware queues
1247 queue_for_each_ctx(q, ctx, i) {
1248 /* If the cpu isn't online, the cpu is mapped to first hctx */
1249 hctx = q->mq_ops->map_queue(q, i);
1250 ctx->index_hw = hctx->nr_ctx;
1251 hctx->ctxs[hctx->nr_ctx++] = ctx;
1255 struct request_queue *blk_mq_init_queue(struct blk_mq_reg *reg,
1258 struct blk_mq_hw_ctx **hctxs;
1259 struct blk_mq_ctx *ctx;
1260 struct request_queue *q;
1263 if (!reg->nr_hw_queues ||
1264 !reg->ops->queue_rq || !reg->ops->map_queue ||
1265 !reg->ops->alloc_hctx || !reg->ops->free_hctx)
1266 return ERR_PTR(-EINVAL);
1268 if (!reg->queue_depth)
1269 reg->queue_depth = BLK_MQ_MAX_DEPTH;
1270 else if (reg->queue_depth > BLK_MQ_MAX_DEPTH) {
1271 pr_err("blk-mq: queuedepth too large (%u)\n", reg->queue_depth);
1272 reg->queue_depth = BLK_MQ_MAX_DEPTH;
1275 if (reg->queue_depth < (reg->reserved_tags + BLK_MQ_TAG_MIN))
1276 return ERR_PTR(-EINVAL);
1278 ctx = alloc_percpu(struct blk_mq_ctx);
1280 return ERR_PTR(-ENOMEM);
1282 hctxs = kmalloc_node(reg->nr_hw_queues * sizeof(*hctxs), GFP_KERNEL,
1288 for (i = 0; i < reg->nr_hw_queues; i++) {
1289 hctxs[i] = reg->ops->alloc_hctx(reg, i);
1293 hctxs[i]->numa_node = NUMA_NO_NODE;
1294 hctxs[i]->queue_num = i;
1297 q = blk_alloc_queue_node(GFP_KERNEL, reg->numa_node);
1301 q->mq_map = blk_mq_make_queue_map(reg);
1305 setup_timer(&q->timeout, blk_mq_rq_timer, (unsigned long) q);
1306 blk_queue_rq_timeout(q, 30000);
1308 q->nr_queues = nr_cpu_ids;
1309 q->nr_hw_queues = reg->nr_hw_queues;
1312 q->queue_hw_ctx = hctxs;
1314 q->mq_ops = reg->ops;
1315 q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
1317 q->sg_reserved_size = INT_MAX;
1319 blk_queue_make_request(q, blk_mq_make_request);
1320 blk_queue_rq_timed_out(q, reg->ops->timeout);
1322 blk_queue_rq_timeout(q, reg->timeout);
1324 if (reg->ops->complete)
1325 blk_queue_softirq_done(q, reg->ops->complete);
1327 blk_mq_init_flush(q);
1328 blk_mq_init_cpu_queues(q, reg->nr_hw_queues);
1330 q->flush_rq = kzalloc(round_up(sizeof(struct request) + reg->cmd_size,
1331 cache_line_size()), GFP_KERNEL);
1335 if (blk_mq_init_hw_queues(q, reg, driver_data))
1338 blk_mq_map_swqueue(q);
1340 mutex_lock(&all_q_mutex);
1341 list_add_tail(&q->all_q_node, &all_q_list);
1342 mutex_unlock(&all_q_mutex);
1351 blk_cleanup_queue(q);
1353 for (i = 0; i < reg->nr_hw_queues; i++) {
1356 reg->ops->free_hctx(hctxs[i], i);
1361 return ERR_PTR(-ENOMEM);
1363 EXPORT_SYMBOL(blk_mq_init_queue);
1365 void blk_mq_free_queue(struct request_queue *q)
1367 struct blk_mq_hw_ctx *hctx;
1370 queue_for_each_hw_ctx(q, hctx, i) {
1371 kfree(hctx->ctx_map);
1373 blk_mq_free_rq_map(hctx);
1374 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
1375 if (q->mq_ops->exit_hctx)
1376 q->mq_ops->exit_hctx(hctx, i);
1377 q->mq_ops->free_hctx(hctx, i);
1380 free_percpu(q->queue_ctx);
1381 kfree(q->queue_hw_ctx);
1384 q->queue_ctx = NULL;
1385 q->queue_hw_ctx = NULL;
1388 mutex_lock(&all_q_mutex);
1389 list_del_init(&q->all_q_node);
1390 mutex_unlock(&all_q_mutex);
1393 /* Basically redo blk_mq_init_queue with queue frozen */
1394 static void blk_mq_queue_reinit(struct request_queue *q)
1396 blk_mq_freeze_queue(q);
1398 blk_mq_update_queue_map(q->mq_map, q->nr_hw_queues);
1401 * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
1402 * we should change hctx numa_node according to new topology (this
1403 * involves free and re-allocate memory, worthy doing?)
1406 blk_mq_map_swqueue(q);
1408 blk_mq_unfreeze_queue(q);
1411 static int blk_mq_queue_reinit_notify(struct notifier_block *nb,
1412 unsigned long action, void *hcpu)
1414 struct request_queue *q;
1417 * Before new mapping is established, hotadded cpu might already start
1418 * handling requests. This doesn't break anything as we map offline
1419 * CPUs to first hardware queue. We will re-init queue below to get
1422 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN &&
1423 action != CPU_ONLINE && action != CPU_ONLINE_FROZEN)
1426 mutex_lock(&all_q_mutex);
1427 list_for_each_entry(q, &all_q_list, all_q_node)
1428 blk_mq_queue_reinit(q);
1429 mutex_unlock(&all_q_mutex);
1433 static int __init blk_mq_init(void)
1437 /* Must be called after percpu_counter_hotcpu_callback() */
1438 hotcpu_notifier(blk_mq_queue_reinit_notify, -10);
1442 subsys_initcall(blk_mq_init);