2 * Block multiqueue core code
4 * Copyright (C) 2013-2014 Jens Axboe
5 * Copyright (C) 2013-2014 Christoph Hellwig
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/backing-dev.h>
10 #include <linux/bio.h>
11 #include <linux/blkdev.h>
13 #include <linux/init.h>
14 #include <linux/slab.h>
15 #include <linux/workqueue.h>
16 #include <linux/smp.h>
17 #include <linux/llist.h>
18 #include <linux/list_sort.h>
19 #include <linux/cpu.h>
20 #include <linux/cache.h>
21 #include <linux/sched/sysctl.h>
22 #include <linux/delay.h>
24 #include <trace/events/block.h>
26 #include <linux/blk-mq.h>
29 #include "blk-mq-tag.h"
31 static DEFINE_MUTEX(all_q_mutex);
32 static LIST_HEAD(all_q_list);
34 static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx);
37 * Check if any of the ctx's have pending work in this hardware queue
39 static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx)
43 for (i = 0; i < hctx->ctx_map.map_size; i++)
44 if (hctx->ctx_map.map[i].word)
50 static inline struct blk_align_bitmap *get_bm(struct blk_mq_hw_ctx *hctx,
51 struct blk_mq_ctx *ctx)
53 return &hctx->ctx_map.map[ctx->index_hw / hctx->ctx_map.bits_per_word];
56 #define CTX_TO_BIT(hctx, ctx) \
57 ((ctx)->index_hw & ((hctx)->ctx_map.bits_per_word - 1))
60 * Mark this ctx as having pending work in this hardware queue
62 static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx,
63 struct blk_mq_ctx *ctx)
65 struct blk_align_bitmap *bm = get_bm(hctx, ctx);
67 if (!test_bit(CTX_TO_BIT(hctx, ctx), &bm->word))
68 set_bit(CTX_TO_BIT(hctx, ctx), &bm->word);
71 static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx *hctx,
72 struct blk_mq_ctx *ctx)
74 struct blk_align_bitmap *bm = get_bm(hctx, ctx);
76 clear_bit(CTX_TO_BIT(hctx, ctx), &bm->word);
79 static int blk_mq_queue_enter(struct request_queue *q)
83 __percpu_counter_add(&q->mq_usage_counter, 1, 1000000);
85 /* we have problems to freeze the queue if it's initializing */
86 if (!blk_queue_bypass(q) || !blk_queue_init_done(q))
89 __percpu_counter_add(&q->mq_usage_counter, -1, 1000000);
91 spin_lock_irq(q->queue_lock);
92 ret = wait_event_interruptible_lock_irq(q->mq_freeze_wq,
93 !blk_queue_bypass(q) || blk_queue_dying(q),
95 /* inc usage with lock hold to avoid freeze_queue runs here */
96 if (!ret && !blk_queue_dying(q))
97 __percpu_counter_add(&q->mq_usage_counter, 1, 1000000);
98 else if (blk_queue_dying(q))
100 spin_unlock_irq(q->queue_lock);
105 static void blk_mq_queue_exit(struct request_queue *q)
107 __percpu_counter_add(&q->mq_usage_counter, -1, 1000000);
110 static void __blk_mq_drain_queue(struct request_queue *q)
115 spin_lock_irq(q->queue_lock);
116 count = percpu_counter_sum(&q->mq_usage_counter);
117 spin_unlock_irq(q->queue_lock);
121 blk_mq_run_queues(q, false);
127 * Guarantee no request is in use, so we can change any data structure of
128 * the queue afterward.
130 static void blk_mq_freeze_queue(struct request_queue *q)
134 spin_lock_irq(q->queue_lock);
135 drain = !q->bypass_depth++;
136 queue_flag_set(QUEUE_FLAG_BYPASS, q);
137 spin_unlock_irq(q->queue_lock);
140 __blk_mq_drain_queue(q);
143 void blk_mq_drain_queue(struct request_queue *q)
145 __blk_mq_drain_queue(q);
148 static void blk_mq_unfreeze_queue(struct request_queue *q)
152 spin_lock_irq(q->queue_lock);
153 if (!--q->bypass_depth) {
154 queue_flag_clear(QUEUE_FLAG_BYPASS, q);
157 WARN_ON_ONCE(q->bypass_depth < 0);
158 spin_unlock_irq(q->queue_lock);
160 wake_up_all(&q->mq_freeze_wq);
163 bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx)
165 return blk_mq_has_free_tags(hctx->tags);
167 EXPORT_SYMBOL(blk_mq_can_queue);
169 static void blk_mq_rq_ctx_init(struct request_queue *q, struct blk_mq_ctx *ctx,
170 struct request *rq, unsigned int rw_flags)
172 if (blk_queue_io_stat(q))
173 rw_flags |= REQ_IO_STAT;
175 INIT_LIST_HEAD(&rq->queuelist);
176 /* csd/requeue_work/fifo_time is initialized before use */
179 rq->cmd_flags |= rw_flags;
180 /* do not touch atomic flags, it needs atomic ops against the timer */
182 INIT_HLIST_NODE(&rq->hash);
183 RB_CLEAR_NODE(&rq->rb_node);
186 #ifdef CONFIG_BLK_CGROUP
188 set_start_time_ns(rq);
189 rq->io_start_time_ns = 0;
191 rq->nr_phys_segments = 0;
192 #if defined(CONFIG_BLK_DEV_INTEGRITY)
193 rq->nr_integrity_segments = 0;
196 /* tag was already set */
204 INIT_LIST_HEAD(&rq->timeout_list);
206 rq->end_io_data = NULL;
209 ctx->rq_dispatched[rw_is_sync(rw_flags)]++;
212 static struct request *
213 __blk_mq_alloc_request(struct request_queue *q, struct blk_mq_hw_ctx *hctx,
214 struct blk_mq_ctx *ctx, int rw, gfp_t gfp, bool reserved)
219 tag = blk_mq_get_tag(hctx, &ctx->last_tag, gfp, reserved);
220 if (tag != BLK_MQ_TAG_FAIL) {
221 rq = hctx->tags->rqs[tag];
224 if (blk_mq_tag_busy(hctx)) {
225 rq->cmd_flags = REQ_MQ_INFLIGHT;
226 atomic_inc(&hctx->nr_active);
230 blk_mq_rq_ctx_init(q, ctx, rq, rw);
237 struct request *blk_mq_alloc_request(struct request_queue *q, int rw, gfp_t gfp,
240 struct blk_mq_ctx *ctx;
241 struct blk_mq_hw_ctx *hctx;
244 if (blk_mq_queue_enter(q))
247 ctx = blk_mq_get_ctx(q);
248 hctx = q->mq_ops->map_queue(q, ctx->cpu);
250 rq = __blk_mq_alloc_request(q, hctx, ctx, rw, gfp & ~__GFP_WAIT,
252 if (!rq && (gfp & __GFP_WAIT)) {
253 __blk_mq_run_hw_queue(hctx);
256 ctx = blk_mq_get_ctx(q);
257 hctx = q->mq_ops->map_queue(q, ctx->cpu);
258 rq = __blk_mq_alloc_request(q, hctx, ctx, rw, gfp, reserved);
263 EXPORT_SYMBOL(blk_mq_alloc_request);
265 static void __blk_mq_free_request(struct blk_mq_hw_ctx *hctx,
266 struct blk_mq_ctx *ctx, struct request *rq)
268 const int tag = rq->tag;
269 struct request_queue *q = rq->q;
271 if (rq->cmd_flags & REQ_MQ_INFLIGHT)
272 atomic_dec(&hctx->nr_active);
274 clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
275 blk_mq_put_tag(hctx, tag, &ctx->last_tag);
276 blk_mq_queue_exit(q);
279 void blk_mq_free_request(struct request *rq)
281 struct blk_mq_ctx *ctx = rq->mq_ctx;
282 struct blk_mq_hw_ctx *hctx;
283 struct request_queue *q = rq->q;
285 ctx->rq_completed[rq_is_sync(rq)]++;
287 hctx = q->mq_ops->map_queue(q, ctx->cpu);
288 __blk_mq_free_request(hctx, ctx, rq);
292 * Clone all relevant state from a request that has been put on hold in
293 * the flush state machine into the preallocated flush request that hangs
294 * off the request queue.
296 * For a driver the flush request should be invisible, that's why we are
297 * impersonating the original request here.
299 void blk_mq_clone_flush_request(struct request *flush_rq,
300 struct request *orig_rq)
302 struct blk_mq_hw_ctx *hctx =
303 orig_rq->q->mq_ops->map_queue(orig_rq->q, orig_rq->mq_ctx->cpu);
305 flush_rq->mq_ctx = orig_rq->mq_ctx;
306 flush_rq->tag = orig_rq->tag;
307 memcpy(blk_mq_rq_to_pdu(flush_rq), blk_mq_rq_to_pdu(orig_rq),
311 inline void __blk_mq_end_io(struct request *rq, int error)
313 blk_account_io_done(rq);
316 rq->end_io(rq, error);
318 if (unlikely(blk_bidi_rq(rq)))
319 blk_mq_free_request(rq->next_rq);
320 blk_mq_free_request(rq);
323 EXPORT_SYMBOL(__blk_mq_end_io);
325 void blk_mq_end_io(struct request *rq, int error)
327 if (blk_update_request(rq, error, blk_rq_bytes(rq)))
329 __blk_mq_end_io(rq, error);
331 EXPORT_SYMBOL(blk_mq_end_io);
333 static void __blk_mq_complete_request_remote(void *data)
335 struct request *rq = data;
337 rq->q->softirq_done_fn(rq);
340 static void blk_mq_ipi_complete_request(struct request *rq)
342 struct blk_mq_ctx *ctx = rq->mq_ctx;
346 if (!test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags)) {
347 rq->q->softirq_done_fn(rq);
352 if (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags))
353 shared = cpus_share_cache(cpu, ctx->cpu);
355 if (cpu != ctx->cpu && !shared && cpu_online(ctx->cpu)) {
356 rq->csd.func = __blk_mq_complete_request_remote;
359 smp_call_function_single_async(ctx->cpu, &rq->csd);
361 rq->q->softirq_done_fn(rq);
366 void __blk_mq_complete_request(struct request *rq)
368 struct request_queue *q = rq->q;
370 if (!q->softirq_done_fn)
371 blk_mq_end_io(rq, rq->errors);
373 blk_mq_ipi_complete_request(rq);
377 * blk_mq_complete_request - end I/O on a request
378 * @rq: the request being processed
381 * Ends all I/O on a request. It does not handle partial completions.
382 * The actual completion happens out-of-order, through a IPI handler.
384 void blk_mq_complete_request(struct request *rq)
386 struct request_queue *q = rq->q;
388 if (unlikely(blk_should_fake_timeout(q)))
390 if (!blk_mark_rq_complete(rq))
391 __blk_mq_complete_request(rq);
393 EXPORT_SYMBOL(blk_mq_complete_request);
395 static void blk_mq_start_request(struct request *rq, bool last)
397 struct request_queue *q = rq->q;
399 trace_block_rq_issue(q, rq);
401 rq->resid_len = blk_rq_bytes(rq);
402 if (unlikely(blk_bidi_rq(rq)))
403 rq->next_rq->resid_len = blk_rq_bytes(rq->next_rq);
406 * Just mark start time and set the started bit. Due to memory
407 * ordering, we know we'll see the correct deadline as long as
408 * REQ_ATOMIC_STARTED is seen. Use the default queue timeout,
409 * unless one has been set in the request.
412 rq->deadline = jiffies + q->rq_timeout;
414 rq->deadline = jiffies + rq->timeout;
417 * Mark us as started and clear complete. Complete might have been
418 * set if requeue raced with timeout, which then marked it as
419 * complete. So be sure to clear complete again when we start
420 * the request, otherwise we'll ignore the completion event.
422 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
423 set_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
424 if (test_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags))
425 clear_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags);
427 if (q->dma_drain_size && blk_rq_bytes(rq)) {
429 * Make sure space for the drain appears. We know we can do
430 * this because max_hw_segments has been adjusted to be one
431 * fewer than the device can handle.
433 rq->nr_phys_segments++;
437 * Flag the last request in the series so that drivers know when IO
438 * should be kicked off, if they don't do it on a per-request basis.
440 * Note: the flag isn't the only condition drivers should do kick off.
441 * If drive is busy, the last request might not have the bit set.
444 rq->cmd_flags |= REQ_END;
447 static void __blk_mq_requeue_request(struct request *rq)
449 struct request_queue *q = rq->q;
451 trace_block_rq_requeue(q, rq);
452 clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
454 rq->cmd_flags &= ~REQ_END;
456 if (q->dma_drain_size && blk_rq_bytes(rq))
457 rq->nr_phys_segments--;
460 void blk_mq_requeue_request(struct request *rq)
462 __blk_mq_requeue_request(rq);
463 blk_clear_rq_complete(rq);
465 BUG_ON(blk_queued_rq(rq));
466 blk_mq_add_to_requeue_list(rq, true);
468 EXPORT_SYMBOL(blk_mq_requeue_request);
470 static void blk_mq_requeue_work(struct work_struct *work)
472 struct request_queue *q =
473 container_of(work, struct request_queue, requeue_work);
475 struct request *rq, *next;
478 spin_lock_irqsave(&q->requeue_lock, flags);
479 list_splice_init(&q->requeue_list, &rq_list);
480 spin_unlock_irqrestore(&q->requeue_lock, flags);
482 list_for_each_entry_safe(rq, next, &rq_list, queuelist) {
483 if (!(rq->cmd_flags & REQ_SOFTBARRIER))
486 rq->cmd_flags &= ~REQ_SOFTBARRIER;
487 list_del_init(&rq->queuelist);
488 blk_mq_insert_request(rq, true, false, false);
491 while (!list_empty(&rq_list)) {
492 rq = list_entry(rq_list.next, struct request, queuelist);
493 list_del_init(&rq->queuelist);
494 blk_mq_insert_request(rq, false, false, false);
497 blk_mq_run_queues(q, false);
500 void blk_mq_add_to_requeue_list(struct request *rq, bool at_head)
502 struct request_queue *q = rq->q;
506 * We abuse this flag that is otherwise used by the I/O scheduler to
507 * request head insertation from the workqueue.
509 BUG_ON(rq->cmd_flags & REQ_SOFTBARRIER);
511 spin_lock_irqsave(&q->requeue_lock, flags);
513 rq->cmd_flags |= REQ_SOFTBARRIER;
514 list_add(&rq->queuelist, &q->requeue_list);
516 list_add_tail(&rq->queuelist, &q->requeue_list);
518 spin_unlock_irqrestore(&q->requeue_lock, flags);
520 EXPORT_SYMBOL(blk_mq_add_to_requeue_list);
522 void blk_mq_kick_requeue_list(struct request_queue *q)
524 kblockd_schedule_work(&q->requeue_work);
526 EXPORT_SYMBOL(blk_mq_kick_requeue_list);
528 struct request *blk_mq_tag_to_rq(struct blk_mq_hw_ctx *hctx, unsigned int tag)
530 struct request_queue *q = hctx->queue;
532 if ((q->flush_rq->cmd_flags & REQ_FLUSH_SEQ) &&
533 q->flush_rq->tag == tag)
536 return hctx->tags->rqs[tag];
538 EXPORT_SYMBOL(blk_mq_tag_to_rq);
540 struct blk_mq_timeout_data {
541 struct blk_mq_hw_ctx *hctx;
543 unsigned int *next_set;
546 static void blk_mq_timeout_check(void *__data, unsigned long *free_tags)
548 struct blk_mq_timeout_data *data = __data;
549 struct blk_mq_hw_ctx *hctx = data->hctx;
552 /* It may not be in flight yet (this is where
553 * the REQ_ATOMIC_STARTED flag comes in). The requests are
554 * statically allocated, so we know it's always safe to access the
555 * memory associated with a bit offset into ->rqs[].
561 tag = find_next_zero_bit(free_tags, hctx->tags->nr_tags, tag);
562 if (tag >= hctx->tags->nr_tags)
565 rq = blk_mq_tag_to_rq(hctx, tag++);
566 if (rq->q != hctx->queue)
568 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
571 blk_rq_check_expired(rq, data->next, data->next_set);
575 static void blk_mq_hw_ctx_check_timeout(struct blk_mq_hw_ctx *hctx,
577 unsigned int *next_set)
579 struct blk_mq_timeout_data data = {
582 .next_set = next_set,
586 * Ask the tagging code to iterate busy requests, so we can
587 * check them for timeout.
589 blk_mq_tag_busy_iter(hctx->tags, blk_mq_timeout_check, &data);
592 static enum blk_eh_timer_return blk_mq_rq_timed_out(struct request *rq)
594 struct request_queue *q = rq->q;
597 * We know that complete is set at this point. If STARTED isn't set
598 * anymore, then the request isn't active and the "timeout" should
599 * just be ignored. This can happen due to the bitflag ordering.
600 * Timeout first checks if STARTED is set, and if it is, assumes
601 * the request is active. But if we race with completion, then
602 * we both flags will get cleared. So check here again, and ignore
603 * a timeout event with a request that isn't active.
605 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
606 return BLK_EH_NOT_HANDLED;
608 if (!q->mq_ops->timeout)
609 return BLK_EH_RESET_TIMER;
611 return q->mq_ops->timeout(rq);
614 static void blk_mq_rq_timer(unsigned long data)
616 struct request_queue *q = (struct request_queue *) data;
617 struct blk_mq_hw_ctx *hctx;
618 unsigned long next = 0;
621 queue_for_each_hw_ctx(q, hctx, i) {
623 * If not software queues are currently mapped to this
624 * hardware queue, there's nothing to check
626 if (!hctx->nr_ctx || !hctx->tags)
629 blk_mq_hw_ctx_check_timeout(hctx, &next, &next_set);
633 next = blk_rq_timeout(round_jiffies_up(next));
634 mod_timer(&q->timeout, next);
636 queue_for_each_hw_ctx(q, hctx, i)
637 blk_mq_tag_idle(hctx);
642 * Reverse check our software queue for entries that we could potentially
643 * merge with. Currently includes a hand-wavy stop count of 8, to not spend
644 * too much time checking for merges.
646 static bool blk_mq_attempt_merge(struct request_queue *q,
647 struct blk_mq_ctx *ctx, struct bio *bio)
652 list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) {
658 if (!blk_rq_merge_ok(rq, bio))
661 el_ret = blk_try_merge(rq, bio);
662 if (el_ret == ELEVATOR_BACK_MERGE) {
663 if (bio_attempt_back_merge(q, rq, bio)) {
668 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
669 if (bio_attempt_front_merge(q, rq, bio)) {
681 * Process software queues that have been marked busy, splicing them
682 * to the for-dispatch
684 static void flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list)
686 struct blk_mq_ctx *ctx;
689 for (i = 0; i < hctx->ctx_map.map_size; i++) {
690 struct blk_align_bitmap *bm = &hctx->ctx_map.map[i];
691 unsigned int off, bit;
697 off = i * hctx->ctx_map.bits_per_word;
699 bit = find_next_bit(&bm->word, bm->depth, bit);
700 if (bit >= bm->depth)
703 ctx = hctx->ctxs[bit + off];
704 clear_bit(bit, &bm->word);
705 spin_lock(&ctx->lock);
706 list_splice_tail_init(&ctx->rq_list, list);
707 spin_unlock(&ctx->lock);
715 * Run this hardware queue, pulling any software queues mapped to it in.
716 * Note that this function currently has various problems around ordering
717 * of IO. In particular, we'd like FIFO behaviour on handling existing
718 * items on the hctx->dispatch list. Ignore that for now.
720 static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
722 struct request_queue *q = hctx->queue;
727 WARN_ON(!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask));
729 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
735 * Touch any software queue that has pending entries.
737 flush_busy_ctxs(hctx, &rq_list);
740 * If we have previous entries on our dispatch list, grab them
741 * and stuff them at the front for more fair dispatch.
743 if (!list_empty_careful(&hctx->dispatch)) {
744 spin_lock(&hctx->lock);
745 if (!list_empty(&hctx->dispatch))
746 list_splice_init(&hctx->dispatch, &rq_list);
747 spin_unlock(&hctx->lock);
751 * Now process all the entries, sending them to the driver.
754 while (!list_empty(&rq_list)) {
757 rq = list_first_entry(&rq_list, struct request, queuelist);
758 list_del_init(&rq->queuelist);
760 blk_mq_start_request(rq, list_empty(&rq_list));
762 ret = q->mq_ops->queue_rq(hctx, rq);
764 case BLK_MQ_RQ_QUEUE_OK:
767 case BLK_MQ_RQ_QUEUE_BUSY:
768 list_add(&rq->queuelist, &rq_list);
769 __blk_mq_requeue_request(rq);
772 pr_err("blk-mq: bad return on queue: %d\n", ret);
773 case BLK_MQ_RQ_QUEUE_ERROR:
775 blk_mq_end_io(rq, rq->errors);
779 if (ret == BLK_MQ_RQ_QUEUE_BUSY)
784 hctx->dispatched[0]++;
785 else if (queued < (1 << (BLK_MQ_MAX_DISPATCH_ORDER - 1)))
786 hctx->dispatched[ilog2(queued) + 1]++;
789 * Any items that need requeuing? Stuff them into hctx->dispatch,
790 * that is where we will continue on next queue run.
792 if (!list_empty(&rq_list)) {
793 spin_lock(&hctx->lock);
794 list_splice(&rq_list, &hctx->dispatch);
795 spin_unlock(&hctx->lock);
800 * It'd be great if the workqueue API had a way to pass
801 * in a mask and had some smarts for more clever placement.
802 * For now we just round-robin here, switching for every
803 * BLK_MQ_CPU_WORK_BATCH queued items.
805 static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx)
807 int cpu = hctx->next_cpu;
809 if (--hctx->next_cpu_batch <= 0) {
812 next_cpu = cpumask_next(hctx->next_cpu, hctx->cpumask);
813 if (next_cpu >= nr_cpu_ids)
814 next_cpu = cpumask_first(hctx->cpumask);
816 hctx->next_cpu = next_cpu;
817 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
823 void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
825 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
828 if (!async && cpumask_test_cpu(smp_processor_id(), hctx->cpumask))
829 __blk_mq_run_hw_queue(hctx);
830 else if (hctx->queue->nr_hw_queues == 1)
831 kblockd_schedule_delayed_work(&hctx->run_work, 0);
835 cpu = blk_mq_hctx_next_cpu(hctx);
836 kblockd_schedule_delayed_work_on(cpu, &hctx->run_work, 0);
840 void blk_mq_run_queues(struct request_queue *q, bool async)
842 struct blk_mq_hw_ctx *hctx;
845 queue_for_each_hw_ctx(q, hctx, i) {
846 if ((!blk_mq_hctx_has_pending(hctx) &&
847 list_empty_careful(&hctx->dispatch)) ||
848 test_bit(BLK_MQ_S_STOPPED, &hctx->state))
852 blk_mq_run_hw_queue(hctx, async);
856 EXPORT_SYMBOL(blk_mq_run_queues);
858 void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
860 cancel_delayed_work(&hctx->run_work);
861 cancel_delayed_work(&hctx->delay_work);
862 set_bit(BLK_MQ_S_STOPPED, &hctx->state);
864 EXPORT_SYMBOL(blk_mq_stop_hw_queue);
866 void blk_mq_stop_hw_queues(struct request_queue *q)
868 struct blk_mq_hw_ctx *hctx;
871 queue_for_each_hw_ctx(q, hctx, i)
872 blk_mq_stop_hw_queue(hctx);
874 EXPORT_SYMBOL(blk_mq_stop_hw_queues);
876 void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
878 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
881 __blk_mq_run_hw_queue(hctx);
884 EXPORT_SYMBOL(blk_mq_start_hw_queue);
886 void blk_mq_start_hw_queues(struct request_queue *q)
888 struct blk_mq_hw_ctx *hctx;
891 queue_for_each_hw_ctx(q, hctx, i)
892 blk_mq_start_hw_queue(hctx);
894 EXPORT_SYMBOL(blk_mq_start_hw_queues);
897 void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async)
899 struct blk_mq_hw_ctx *hctx;
902 queue_for_each_hw_ctx(q, hctx, i) {
903 if (!test_bit(BLK_MQ_S_STOPPED, &hctx->state))
906 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
908 blk_mq_run_hw_queue(hctx, async);
912 EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
914 static void blk_mq_run_work_fn(struct work_struct *work)
916 struct blk_mq_hw_ctx *hctx;
918 hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work);
920 __blk_mq_run_hw_queue(hctx);
923 static void blk_mq_delay_work_fn(struct work_struct *work)
925 struct blk_mq_hw_ctx *hctx;
927 hctx = container_of(work, struct blk_mq_hw_ctx, delay_work.work);
929 if (test_and_clear_bit(BLK_MQ_S_STOPPED, &hctx->state))
930 __blk_mq_run_hw_queue(hctx);
933 void blk_mq_delay_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
935 unsigned long tmo = msecs_to_jiffies(msecs);
937 if (hctx->queue->nr_hw_queues == 1)
938 kblockd_schedule_delayed_work(&hctx->delay_work, tmo);
942 cpu = blk_mq_hctx_next_cpu(hctx);
943 kblockd_schedule_delayed_work_on(cpu, &hctx->delay_work, tmo);
946 EXPORT_SYMBOL(blk_mq_delay_queue);
948 static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx,
949 struct request *rq, bool at_head)
951 struct blk_mq_ctx *ctx = rq->mq_ctx;
953 trace_block_rq_insert(hctx->queue, rq);
956 list_add(&rq->queuelist, &ctx->rq_list);
958 list_add_tail(&rq->queuelist, &ctx->rq_list);
960 blk_mq_hctx_mark_pending(hctx, ctx);
963 * We do this early, to ensure we are on the right CPU.
968 void blk_mq_insert_request(struct request *rq, bool at_head, bool run_queue,
971 struct request_queue *q = rq->q;
972 struct blk_mq_hw_ctx *hctx;
973 struct blk_mq_ctx *ctx = rq->mq_ctx, *current_ctx;
975 current_ctx = blk_mq_get_ctx(q);
976 if (!cpu_online(ctx->cpu))
977 rq->mq_ctx = ctx = current_ctx;
979 hctx = q->mq_ops->map_queue(q, ctx->cpu);
981 if (rq->cmd_flags & (REQ_FLUSH | REQ_FUA) &&
982 !(rq->cmd_flags & (REQ_FLUSH_SEQ))) {
983 blk_insert_flush(rq);
985 spin_lock(&ctx->lock);
986 __blk_mq_insert_request(hctx, rq, at_head);
987 spin_unlock(&ctx->lock);
991 blk_mq_run_hw_queue(hctx, async);
993 blk_mq_put_ctx(current_ctx);
996 static void blk_mq_insert_requests(struct request_queue *q,
997 struct blk_mq_ctx *ctx,
998 struct list_head *list,
1003 struct blk_mq_hw_ctx *hctx;
1004 struct blk_mq_ctx *current_ctx;
1006 trace_block_unplug(q, depth, !from_schedule);
1008 current_ctx = blk_mq_get_ctx(q);
1010 if (!cpu_online(ctx->cpu))
1012 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1015 * preemption doesn't flush plug list, so it's possible ctx->cpu is
1018 spin_lock(&ctx->lock);
1019 while (!list_empty(list)) {
1022 rq = list_first_entry(list, struct request, queuelist);
1023 list_del_init(&rq->queuelist);
1025 __blk_mq_insert_request(hctx, rq, false);
1027 spin_unlock(&ctx->lock);
1029 blk_mq_run_hw_queue(hctx, from_schedule);
1030 blk_mq_put_ctx(current_ctx);
1033 static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b)
1035 struct request *rqa = container_of(a, struct request, queuelist);
1036 struct request *rqb = container_of(b, struct request, queuelist);
1038 return !(rqa->mq_ctx < rqb->mq_ctx ||
1039 (rqa->mq_ctx == rqb->mq_ctx &&
1040 blk_rq_pos(rqa) < blk_rq_pos(rqb)));
1043 void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
1045 struct blk_mq_ctx *this_ctx;
1046 struct request_queue *this_q;
1049 LIST_HEAD(ctx_list);
1052 list_splice_init(&plug->mq_list, &list);
1054 list_sort(NULL, &list, plug_ctx_cmp);
1060 while (!list_empty(&list)) {
1061 rq = list_entry_rq(list.next);
1062 list_del_init(&rq->queuelist);
1064 if (rq->mq_ctx != this_ctx) {
1066 blk_mq_insert_requests(this_q, this_ctx,
1071 this_ctx = rq->mq_ctx;
1077 list_add_tail(&rq->queuelist, &ctx_list);
1081 * If 'this_ctx' is set, we know we have entries to complete
1082 * on 'ctx_list'. Do those.
1085 blk_mq_insert_requests(this_q, this_ctx, &ctx_list, depth,
1090 static void blk_mq_bio_to_request(struct request *rq, struct bio *bio)
1092 init_request_from_bio(rq, bio);
1094 if (blk_do_io_stat(rq)) {
1095 rq->start_time = jiffies;
1096 blk_account_io_start(rq, 1);
1100 static inline bool blk_mq_merge_queue_io(struct blk_mq_hw_ctx *hctx,
1101 struct blk_mq_ctx *ctx,
1102 struct request *rq, struct bio *bio)
1104 struct request_queue *q = hctx->queue;
1106 if (!(hctx->flags & BLK_MQ_F_SHOULD_MERGE)) {
1107 blk_mq_bio_to_request(rq, bio);
1108 spin_lock(&ctx->lock);
1110 __blk_mq_insert_request(hctx, rq, false);
1111 spin_unlock(&ctx->lock);
1114 spin_lock(&ctx->lock);
1115 if (!blk_mq_attempt_merge(q, ctx, bio)) {
1116 blk_mq_bio_to_request(rq, bio);
1120 spin_unlock(&ctx->lock);
1121 __blk_mq_free_request(hctx, ctx, rq);
1126 struct blk_map_ctx {
1127 struct blk_mq_hw_ctx *hctx;
1128 struct blk_mq_ctx *ctx;
1131 static struct request *blk_mq_map_request(struct request_queue *q,
1133 struct blk_map_ctx *data)
1135 struct blk_mq_hw_ctx *hctx;
1136 struct blk_mq_ctx *ctx;
1138 int rw = bio_data_dir(bio);
1140 if (unlikely(blk_mq_queue_enter(q))) {
1141 bio_endio(bio, -EIO);
1145 ctx = blk_mq_get_ctx(q);
1146 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1148 if (rw_is_sync(bio->bi_rw))
1151 trace_block_getrq(q, bio, rw);
1152 rq = __blk_mq_alloc_request(q, hctx, ctx, rw, GFP_ATOMIC, false);
1153 if (unlikely(!rq)) {
1154 __blk_mq_run_hw_queue(hctx);
1155 blk_mq_put_ctx(ctx);
1156 trace_block_sleeprq(q, bio, rw);
1158 ctx = blk_mq_get_ctx(q);
1159 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1160 rq = __blk_mq_alloc_request(q, hctx, ctx, rw,
1161 __GFP_WAIT|GFP_ATOMIC, false);
1171 * Multiple hardware queue variant. This will not use per-process plugs,
1172 * but will attempt to bypass the hctx queueing if we can go straight to
1173 * hardware for SYNC IO.
1175 static void blk_mq_make_request(struct request_queue *q, struct bio *bio)
1177 const int is_sync = rw_is_sync(bio->bi_rw);
1178 const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
1179 struct blk_map_ctx data;
1182 blk_queue_bounce(q, &bio);
1184 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
1185 bio_endio(bio, -EIO);
1189 rq = blk_mq_map_request(q, bio, &data);
1193 if (unlikely(is_flush_fua)) {
1194 blk_mq_bio_to_request(rq, bio);
1195 blk_insert_flush(rq);
1202 blk_mq_bio_to_request(rq, bio);
1203 blk_mq_start_request(rq, true);
1207 * For OK queue, we are done. For error, kill it. Any other
1208 * error (busy), just add it to our list as we previously
1211 ret = q->mq_ops->queue_rq(data.hctx, rq);
1212 if (ret == BLK_MQ_RQ_QUEUE_OK)
1215 __blk_mq_requeue_request(rq);
1217 if (ret == BLK_MQ_RQ_QUEUE_ERROR) {
1219 blk_mq_end_io(rq, rq->errors);
1225 if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) {
1227 * For a SYNC request, send it to the hardware immediately. For
1228 * an ASYNC request, just ensure that we run it later on. The
1229 * latter allows for merging opportunities and more efficient
1233 blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua);
1236 blk_mq_put_ctx(data.ctx);
1240 * Single hardware queue variant. This will attempt to use any per-process
1241 * plug for merging and IO deferral.
1243 static void blk_sq_make_request(struct request_queue *q, struct bio *bio)
1245 const int is_sync = rw_is_sync(bio->bi_rw);
1246 const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
1247 unsigned int use_plug, request_count = 0;
1248 struct blk_map_ctx data;
1252 * If we have multiple hardware queues, just go directly to
1253 * one of those for sync IO.
1255 use_plug = !is_flush_fua && !is_sync;
1257 blk_queue_bounce(q, &bio);
1259 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
1260 bio_endio(bio, -EIO);
1264 if (use_plug && !blk_queue_nomerges(q) &&
1265 blk_attempt_plug_merge(q, bio, &request_count))
1268 rq = blk_mq_map_request(q, bio, &data);
1270 if (unlikely(is_flush_fua)) {
1271 blk_mq_bio_to_request(rq, bio);
1272 blk_insert_flush(rq);
1277 * A task plug currently exists. Since this is completely lockless,
1278 * utilize that to temporarily store requests until the task is
1279 * either done or scheduled away.
1282 struct blk_plug *plug = current->plug;
1285 blk_mq_bio_to_request(rq, bio);
1286 if (list_empty(&plug->mq_list))
1287 trace_block_plug(q);
1288 else if (request_count >= BLK_MAX_REQUEST_COUNT) {
1289 blk_flush_plug_list(plug, false);
1290 trace_block_plug(q);
1292 list_add_tail(&rq->queuelist, &plug->mq_list);
1293 blk_mq_put_ctx(data.ctx);
1298 if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) {
1300 * For a SYNC request, send it to the hardware immediately. For
1301 * an ASYNC request, just ensure that we run it later on. The
1302 * latter allows for merging opportunities and more efficient
1306 blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua);
1309 blk_mq_put_ctx(data.ctx);
1313 * Default mapping to a software queue, since we use one per CPU.
1315 struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q, const int cpu)
1317 return q->queue_hw_ctx[q->mq_map[cpu]];
1319 EXPORT_SYMBOL(blk_mq_map_queue);
1321 static void blk_mq_free_rq_map(struct blk_mq_tag_set *set,
1322 struct blk_mq_tags *tags, unsigned int hctx_idx)
1326 if (tags->rqs && set->ops->exit_request) {
1329 for (i = 0; i < tags->nr_tags; i++) {
1332 set->ops->exit_request(set->driver_data, tags->rqs[i],
1337 while (!list_empty(&tags->page_list)) {
1338 page = list_first_entry(&tags->page_list, struct page, lru);
1339 list_del_init(&page->lru);
1340 __free_pages(page, page->private);
1345 blk_mq_free_tags(tags);
1348 static size_t order_to_size(unsigned int order)
1350 return (size_t)PAGE_SIZE << order;
1353 static struct blk_mq_tags *blk_mq_init_rq_map(struct blk_mq_tag_set *set,
1354 unsigned int hctx_idx)
1356 struct blk_mq_tags *tags;
1357 unsigned int i, j, entries_per_page, max_order = 4;
1358 size_t rq_size, left;
1360 tags = blk_mq_init_tags(set->queue_depth, set->reserved_tags,
1365 INIT_LIST_HEAD(&tags->page_list);
1367 tags->rqs = kmalloc_node(set->queue_depth * sizeof(struct request *),
1368 GFP_KERNEL, set->numa_node);
1370 blk_mq_free_tags(tags);
1375 * rq_size is the size of the request plus driver payload, rounded
1376 * to the cacheline size
1378 rq_size = round_up(sizeof(struct request) + set->cmd_size,
1380 left = rq_size * set->queue_depth;
1382 for (i = 0; i < set->queue_depth; ) {
1383 int this_order = max_order;
1388 while (left < order_to_size(this_order - 1) && this_order)
1392 page = alloc_pages_node(set->numa_node, GFP_KERNEL,
1398 if (order_to_size(this_order) < rq_size)
1405 page->private = this_order;
1406 list_add_tail(&page->lru, &tags->page_list);
1408 p = page_address(page);
1409 entries_per_page = order_to_size(this_order) / rq_size;
1410 to_do = min(entries_per_page, set->queue_depth - i);
1411 left -= to_do * rq_size;
1412 for (j = 0; j < to_do; j++) {
1414 if (set->ops->init_request) {
1415 if (set->ops->init_request(set->driver_data,
1416 tags->rqs[i], hctx_idx, i,
1429 pr_warn("%s: failed to allocate requests\n", __func__);
1430 blk_mq_free_rq_map(set, tags, hctx_idx);
1434 static void blk_mq_free_bitmap(struct blk_mq_ctxmap *bitmap)
1439 static int blk_mq_alloc_bitmap(struct blk_mq_ctxmap *bitmap, int node)
1441 unsigned int bpw = 8, total, num_maps, i;
1443 bitmap->bits_per_word = bpw;
1445 num_maps = ALIGN(nr_cpu_ids, bpw) / bpw;
1446 bitmap->map = kzalloc_node(num_maps * sizeof(struct blk_align_bitmap),
1451 bitmap->map_size = num_maps;
1454 for (i = 0; i < num_maps; i++) {
1455 bitmap->map[i].depth = min(total, bitmap->bits_per_word);
1456 total -= bitmap->map[i].depth;
1462 static int blk_mq_hctx_cpu_offline(struct blk_mq_hw_ctx *hctx, int cpu)
1464 struct request_queue *q = hctx->queue;
1465 struct blk_mq_ctx *ctx;
1469 * Move ctx entries to new CPU, if this one is going away.
1471 ctx = __blk_mq_get_ctx(q, cpu);
1473 spin_lock(&ctx->lock);
1474 if (!list_empty(&ctx->rq_list)) {
1475 list_splice_init(&ctx->rq_list, &tmp);
1476 blk_mq_hctx_clear_pending(hctx, ctx);
1478 spin_unlock(&ctx->lock);
1480 if (list_empty(&tmp))
1483 ctx = blk_mq_get_ctx(q);
1484 spin_lock(&ctx->lock);
1486 while (!list_empty(&tmp)) {
1489 rq = list_first_entry(&tmp, struct request, queuelist);
1491 list_move_tail(&rq->queuelist, &ctx->rq_list);
1494 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1495 blk_mq_hctx_mark_pending(hctx, ctx);
1497 spin_unlock(&ctx->lock);
1499 blk_mq_run_hw_queue(hctx, true);
1500 blk_mq_put_ctx(ctx);
1504 static int blk_mq_hctx_cpu_online(struct blk_mq_hw_ctx *hctx, int cpu)
1506 struct request_queue *q = hctx->queue;
1507 struct blk_mq_tag_set *set = q->tag_set;
1509 if (set->tags[hctx->queue_num])
1512 set->tags[hctx->queue_num] = blk_mq_init_rq_map(set, hctx->queue_num);
1513 if (!set->tags[hctx->queue_num])
1516 hctx->tags = set->tags[hctx->queue_num];
1520 static int blk_mq_hctx_notify(void *data, unsigned long action,
1523 struct blk_mq_hw_ctx *hctx = data;
1525 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN)
1526 return blk_mq_hctx_cpu_offline(hctx, cpu);
1527 else if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN)
1528 return blk_mq_hctx_cpu_online(hctx, cpu);
1533 static void blk_mq_exit_hw_queues(struct request_queue *q,
1534 struct blk_mq_tag_set *set, int nr_queue)
1536 struct blk_mq_hw_ctx *hctx;
1539 queue_for_each_hw_ctx(q, hctx, i) {
1543 if (set->ops->exit_hctx)
1544 set->ops->exit_hctx(hctx, i);
1546 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
1548 blk_mq_free_bitmap(&hctx->ctx_map);
1553 static void blk_mq_free_hw_queues(struct request_queue *q,
1554 struct blk_mq_tag_set *set)
1556 struct blk_mq_hw_ctx *hctx;
1559 queue_for_each_hw_ctx(q, hctx, i) {
1560 free_cpumask_var(hctx->cpumask);
1565 static int blk_mq_init_hw_queues(struct request_queue *q,
1566 struct blk_mq_tag_set *set)
1568 struct blk_mq_hw_ctx *hctx;
1572 * Initialize hardware queues
1574 queue_for_each_hw_ctx(q, hctx, i) {
1577 node = hctx->numa_node;
1578 if (node == NUMA_NO_NODE)
1579 node = hctx->numa_node = set->numa_node;
1581 INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn);
1582 INIT_DELAYED_WORK(&hctx->delay_work, blk_mq_delay_work_fn);
1583 spin_lock_init(&hctx->lock);
1584 INIT_LIST_HEAD(&hctx->dispatch);
1586 hctx->queue_num = i;
1587 hctx->flags = set->flags;
1588 hctx->cmd_size = set->cmd_size;
1590 blk_mq_init_cpu_notifier(&hctx->cpu_notifier,
1591 blk_mq_hctx_notify, hctx);
1592 blk_mq_register_cpu_notifier(&hctx->cpu_notifier);
1594 hctx->tags = set->tags[i];
1597 * Allocate space for all possible cpus to avoid allocation in
1600 hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *),
1605 if (blk_mq_alloc_bitmap(&hctx->ctx_map, node))
1610 if (set->ops->init_hctx &&
1611 set->ops->init_hctx(hctx, set->driver_data, i))
1615 if (i == q->nr_hw_queues)
1621 blk_mq_exit_hw_queues(q, set, i);
1626 static void blk_mq_init_cpu_queues(struct request_queue *q,
1627 unsigned int nr_hw_queues)
1631 for_each_possible_cpu(i) {
1632 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
1633 struct blk_mq_hw_ctx *hctx;
1635 memset(__ctx, 0, sizeof(*__ctx));
1637 spin_lock_init(&__ctx->lock);
1638 INIT_LIST_HEAD(&__ctx->rq_list);
1641 /* If the cpu isn't online, the cpu is mapped to first hctx */
1645 hctx = q->mq_ops->map_queue(q, i);
1646 cpumask_set_cpu(i, hctx->cpumask);
1650 * Set local node, IFF we have more than one hw queue. If
1651 * not, we remain on the home node of the device
1653 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
1654 hctx->numa_node = cpu_to_node(i);
1658 static void blk_mq_map_swqueue(struct request_queue *q)
1661 struct blk_mq_hw_ctx *hctx;
1662 struct blk_mq_ctx *ctx;
1664 queue_for_each_hw_ctx(q, hctx, i) {
1665 cpumask_clear(hctx->cpumask);
1670 * Map software to hardware queues
1672 queue_for_each_ctx(q, ctx, i) {
1673 /* If the cpu isn't online, the cpu is mapped to first hctx */
1677 hctx = q->mq_ops->map_queue(q, i);
1678 cpumask_set_cpu(i, hctx->cpumask);
1679 ctx->index_hw = hctx->nr_ctx;
1680 hctx->ctxs[hctx->nr_ctx++] = ctx;
1683 queue_for_each_hw_ctx(q, hctx, i) {
1685 * If not software queues are mapped to this hardware queue,
1686 * disable it and free the request entries
1688 if (!hctx->nr_ctx) {
1689 struct blk_mq_tag_set *set = q->tag_set;
1692 blk_mq_free_rq_map(set, set->tags[i], i);
1693 set->tags[i] = NULL;
1700 * Initialize batch roundrobin counts
1702 hctx->next_cpu = cpumask_first(hctx->cpumask);
1703 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
1707 static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set *set)
1709 struct blk_mq_hw_ctx *hctx;
1710 struct request_queue *q;
1714 if (set->tag_list.next == set->tag_list.prev)
1719 list_for_each_entry(q, &set->tag_list, tag_set_list) {
1720 blk_mq_freeze_queue(q);
1722 queue_for_each_hw_ctx(q, hctx, i) {
1724 hctx->flags |= BLK_MQ_F_TAG_SHARED;
1726 hctx->flags &= ~BLK_MQ_F_TAG_SHARED;
1728 blk_mq_unfreeze_queue(q);
1732 static void blk_mq_del_queue_tag_set(struct request_queue *q)
1734 struct blk_mq_tag_set *set = q->tag_set;
1736 blk_mq_freeze_queue(q);
1738 mutex_lock(&set->tag_list_lock);
1739 list_del_init(&q->tag_set_list);
1740 blk_mq_update_tag_set_depth(set);
1741 mutex_unlock(&set->tag_list_lock);
1743 blk_mq_unfreeze_queue(q);
1746 static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set,
1747 struct request_queue *q)
1751 mutex_lock(&set->tag_list_lock);
1752 list_add_tail(&q->tag_set_list, &set->tag_list);
1753 blk_mq_update_tag_set_depth(set);
1754 mutex_unlock(&set->tag_list_lock);
1757 struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
1759 struct blk_mq_hw_ctx **hctxs;
1760 struct blk_mq_ctx *ctx;
1761 struct request_queue *q;
1765 ctx = alloc_percpu(struct blk_mq_ctx);
1767 return ERR_PTR(-ENOMEM);
1769 hctxs = kmalloc_node(set->nr_hw_queues * sizeof(*hctxs), GFP_KERNEL,
1775 map = blk_mq_make_queue_map(set);
1779 for (i = 0; i < set->nr_hw_queues; i++) {
1780 int node = blk_mq_hw_queue_to_node(map, i);
1782 hctxs[i] = kzalloc_node(sizeof(struct blk_mq_hw_ctx),
1787 if (!zalloc_cpumask_var(&hctxs[i]->cpumask, GFP_KERNEL))
1790 atomic_set(&hctxs[i]->nr_active, 0);
1791 hctxs[i]->numa_node = node;
1792 hctxs[i]->queue_num = i;
1795 q = blk_alloc_queue_node(GFP_KERNEL, set->numa_node);
1799 if (percpu_counter_init(&q->mq_usage_counter, 0))
1802 setup_timer(&q->timeout, blk_mq_rq_timer, (unsigned long) q);
1803 blk_queue_rq_timeout(q, 30000);
1805 q->nr_queues = nr_cpu_ids;
1806 q->nr_hw_queues = set->nr_hw_queues;
1810 q->queue_hw_ctx = hctxs;
1812 q->mq_ops = set->ops;
1813 q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
1815 if (!(set->flags & BLK_MQ_F_SG_MERGE))
1816 q->queue_flags |= 1 << QUEUE_FLAG_NO_SG_MERGE;
1818 q->sg_reserved_size = INT_MAX;
1820 INIT_WORK(&q->requeue_work, blk_mq_requeue_work);
1821 INIT_LIST_HEAD(&q->requeue_list);
1822 spin_lock_init(&q->requeue_lock);
1824 if (q->nr_hw_queues > 1)
1825 blk_queue_make_request(q, blk_mq_make_request);
1827 blk_queue_make_request(q, blk_sq_make_request);
1829 blk_queue_rq_timed_out(q, blk_mq_rq_timed_out);
1831 blk_queue_rq_timeout(q, set->timeout);
1834 * Do this after blk_queue_make_request() overrides it...
1836 q->nr_requests = set->queue_depth;
1838 if (set->ops->complete)
1839 blk_queue_softirq_done(q, set->ops->complete);
1841 blk_mq_init_flush(q);
1842 blk_mq_init_cpu_queues(q, set->nr_hw_queues);
1844 q->flush_rq = kzalloc(round_up(sizeof(struct request) +
1845 set->cmd_size, cache_line_size()),
1850 if (blk_mq_init_hw_queues(q, set))
1853 mutex_lock(&all_q_mutex);
1854 list_add_tail(&q->all_q_node, &all_q_list);
1855 mutex_unlock(&all_q_mutex);
1857 blk_mq_add_queue_tag_set(set, q);
1859 blk_mq_map_swqueue(q);
1866 blk_cleanup_queue(q);
1869 for (i = 0; i < set->nr_hw_queues; i++) {
1872 free_cpumask_var(hctxs[i]->cpumask);
1879 return ERR_PTR(-ENOMEM);
1881 EXPORT_SYMBOL(blk_mq_init_queue);
1883 void blk_mq_free_queue(struct request_queue *q)
1885 struct blk_mq_tag_set *set = q->tag_set;
1887 blk_mq_del_queue_tag_set(q);
1889 blk_mq_exit_hw_queues(q, set, set->nr_hw_queues);
1890 blk_mq_free_hw_queues(q, set);
1892 percpu_counter_destroy(&q->mq_usage_counter);
1894 free_percpu(q->queue_ctx);
1895 kfree(q->queue_hw_ctx);
1898 q->queue_ctx = NULL;
1899 q->queue_hw_ctx = NULL;
1902 mutex_lock(&all_q_mutex);
1903 list_del_init(&q->all_q_node);
1904 mutex_unlock(&all_q_mutex);
1907 /* Basically redo blk_mq_init_queue with queue frozen */
1908 static void blk_mq_queue_reinit(struct request_queue *q)
1910 blk_mq_freeze_queue(q);
1912 blk_mq_sysfs_unregister(q);
1914 blk_mq_update_queue_map(q->mq_map, q->nr_hw_queues);
1917 * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
1918 * we should change hctx numa_node according to new topology (this
1919 * involves free and re-allocate memory, worthy doing?)
1922 blk_mq_map_swqueue(q);
1924 blk_mq_sysfs_register(q);
1926 blk_mq_unfreeze_queue(q);
1929 static int blk_mq_queue_reinit_notify(struct notifier_block *nb,
1930 unsigned long action, void *hcpu)
1932 struct request_queue *q;
1935 * Before new mappings are established, hotadded cpu might already
1936 * start handling requests. This doesn't break anything as we map
1937 * offline CPUs to first hardware queue. We will re-init the queue
1938 * below to get optimal settings.
1940 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN &&
1941 action != CPU_ONLINE && action != CPU_ONLINE_FROZEN)
1944 mutex_lock(&all_q_mutex);
1945 list_for_each_entry(q, &all_q_list, all_q_node)
1946 blk_mq_queue_reinit(q);
1947 mutex_unlock(&all_q_mutex);
1951 int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
1955 if (!set->nr_hw_queues)
1957 if (!set->queue_depth || set->queue_depth > BLK_MQ_MAX_DEPTH)
1959 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN)
1962 if (!set->nr_hw_queues || !set->ops->queue_rq || !set->ops->map_queue)
1966 set->tags = kmalloc_node(set->nr_hw_queues *
1967 sizeof(struct blk_mq_tags *),
1968 GFP_KERNEL, set->numa_node);
1972 for (i = 0; i < set->nr_hw_queues; i++) {
1973 set->tags[i] = blk_mq_init_rq_map(set, i);
1978 mutex_init(&set->tag_list_lock);
1979 INIT_LIST_HEAD(&set->tag_list);
1985 blk_mq_free_rq_map(set, set->tags[i], i);
1989 EXPORT_SYMBOL(blk_mq_alloc_tag_set);
1991 void blk_mq_free_tag_set(struct blk_mq_tag_set *set)
1995 for (i = 0; i < set->nr_hw_queues; i++) {
1997 blk_mq_free_rq_map(set, set->tags[i], i);
2002 EXPORT_SYMBOL(blk_mq_free_tag_set);
2004 int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr)
2006 struct blk_mq_tag_set *set = q->tag_set;
2007 struct blk_mq_hw_ctx *hctx;
2010 if (!set || nr > set->queue_depth)
2014 queue_for_each_hw_ctx(q, hctx, i) {
2015 ret = blk_mq_tag_update_depth(hctx->tags, nr);
2021 q->nr_requests = nr;
2026 void blk_mq_disable_hotplug(void)
2028 mutex_lock(&all_q_mutex);
2031 void blk_mq_enable_hotplug(void)
2033 mutex_unlock(&all_q_mutex);
2036 static int __init blk_mq_init(void)
2040 /* Must be called after percpu_counter_hotcpu_callback() */
2041 hotcpu_notifier(blk_mq_queue_reinit_notify, -10);
2045 subsys_initcall(blk_mq_init);