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);
36 static struct blk_mq_ctx *__blk_mq_get_ctx(struct request_queue *q,
39 return per_cpu_ptr(q->queue_ctx, cpu);
43 * This assumes per-cpu software queueing queues. They could be per-node
44 * as well, for instance. For now this is hardcoded as-is. Note that we don't
45 * care about preemption, since we know the ctx's are persistent. This does
46 * mean that we can't rely on ctx always matching the currently running CPU.
48 static struct blk_mq_ctx *blk_mq_get_ctx(struct request_queue *q)
50 return __blk_mq_get_ctx(q, get_cpu());
53 static void blk_mq_put_ctx(struct blk_mq_ctx *ctx)
59 * Check if any of the ctx's have pending work in this hardware queue
61 static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx)
65 for (i = 0; i < hctx->ctx_map.map_size; i++)
66 if (hctx->ctx_map.map[i].word)
72 static inline struct blk_align_bitmap *get_bm(struct blk_mq_hw_ctx *hctx,
73 struct blk_mq_ctx *ctx)
75 return &hctx->ctx_map.map[ctx->index_hw / hctx->ctx_map.bits_per_word];
78 #define CTX_TO_BIT(hctx, ctx) \
79 ((ctx)->index_hw & ((hctx)->ctx_map.bits_per_word - 1))
82 * Mark this ctx as having pending work in this hardware queue
84 static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx,
85 struct blk_mq_ctx *ctx)
87 struct blk_align_bitmap *bm = get_bm(hctx, ctx);
89 if (!test_bit(CTX_TO_BIT(hctx, ctx), &bm->word))
90 set_bit(CTX_TO_BIT(hctx, ctx), &bm->word);
93 static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx *hctx,
94 struct blk_mq_ctx *ctx)
96 struct blk_align_bitmap *bm = get_bm(hctx, ctx);
98 clear_bit(CTX_TO_BIT(hctx, ctx), &bm->word);
101 static int blk_mq_queue_enter(struct request_queue *q)
105 __percpu_counter_add(&q->mq_usage_counter, 1, 1000000);
107 /* we have problems to freeze the queue if it's initializing */
108 if (!blk_queue_bypass(q) || !blk_queue_init_done(q))
111 __percpu_counter_add(&q->mq_usage_counter, -1, 1000000);
113 spin_lock_irq(q->queue_lock);
114 ret = wait_event_interruptible_lock_irq(q->mq_freeze_wq,
115 !blk_queue_bypass(q) || blk_queue_dying(q),
117 /* inc usage with lock hold to avoid freeze_queue runs here */
118 if (!ret && !blk_queue_dying(q))
119 __percpu_counter_add(&q->mq_usage_counter, 1, 1000000);
120 else if (blk_queue_dying(q))
122 spin_unlock_irq(q->queue_lock);
127 static void blk_mq_queue_exit(struct request_queue *q)
129 __percpu_counter_add(&q->mq_usage_counter, -1, 1000000);
132 static void __blk_mq_drain_queue(struct request_queue *q)
137 spin_lock_irq(q->queue_lock);
138 count = percpu_counter_sum(&q->mq_usage_counter);
139 spin_unlock_irq(q->queue_lock);
143 blk_mq_run_queues(q, false);
149 * Guarantee no request is in use, so we can change any data structure of
150 * the queue afterward.
152 static void blk_mq_freeze_queue(struct request_queue *q)
156 spin_lock_irq(q->queue_lock);
157 drain = !q->bypass_depth++;
158 queue_flag_set(QUEUE_FLAG_BYPASS, q);
159 spin_unlock_irq(q->queue_lock);
162 __blk_mq_drain_queue(q);
165 void blk_mq_drain_queue(struct request_queue *q)
167 __blk_mq_drain_queue(q);
170 static void blk_mq_unfreeze_queue(struct request_queue *q)
174 spin_lock_irq(q->queue_lock);
175 if (!--q->bypass_depth) {
176 queue_flag_clear(QUEUE_FLAG_BYPASS, q);
179 WARN_ON_ONCE(q->bypass_depth < 0);
180 spin_unlock_irq(q->queue_lock);
182 wake_up_all(&q->mq_freeze_wq);
185 bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx)
187 return blk_mq_has_free_tags(hctx->tags);
189 EXPORT_SYMBOL(blk_mq_can_queue);
191 static void blk_mq_rq_ctx_init(struct request_queue *q, struct blk_mq_ctx *ctx,
192 struct request *rq, unsigned int rw_flags)
194 if (blk_queue_io_stat(q))
195 rw_flags |= REQ_IO_STAT;
197 INIT_LIST_HEAD(&rq->queuelist);
198 /* csd/requeue_work/fifo_time is initialized before use */
201 rq->cmd_flags |= rw_flags;
203 /* do not touch atomic flags, it needs atomic ops against the timer */
206 rq->__sector = (sector_t) -1;
209 INIT_HLIST_NODE(&rq->hash);
210 RB_CLEAR_NODE(&rq->rb_node);
211 memset(&rq->flush, 0, max(sizeof(rq->flush), sizeof(rq->elv)));
214 rq->start_time = jiffies;
215 #ifdef CONFIG_BLK_CGROUP
217 set_start_time_ns(rq);
218 rq->io_start_time_ns = 0;
220 rq->nr_phys_segments = 0;
221 #if defined(CONFIG_BLK_DEV_INTEGRITY)
222 rq->nr_integrity_segments = 0;
226 /* tag was already set */
228 memset(rq->__cmd, 0, sizeof(rq->__cmd));
230 rq->cmd_len = BLK_MAX_CDB;
238 INIT_LIST_HEAD(&rq->timeout_list);
242 rq->end_io_data = NULL;
245 ctx->rq_dispatched[rw_is_sync(rw_flags)]++;
248 static struct request *
249 __blk_mq_alloc_request(struct request_queue *q, struct blk_mq_hw_ctx *hctx,
250 struct blk_mq_ctx *ctx, int rw, gfp_t gfp, bool reserved)
255 tag = blk_mq_get_tag(hctx, &ctx->last_tag, gfp, reserved);
256 if (tag != BLK_MQ_TAG_FAIL) {
257 rq = hctx->tags->rqs[tag];
260 if (blk_mq_tag_busy(hctx)) {
261 rq->cmd_flags = REQ_MQ_INFLIGHT;
262 atomic_inc(&hctx->nr_active);
266 blk_mq_rq_ctx_init(q, ctx, rq, rw);
273 struct request *blk_mq_alloc_request(struct request_queue *q, int rw, gfp_t gfp,
276 struct blk_mq_ctx *ctx;
277 struct blk_mq_hw_ctx *hctx;
280 if (blk_mq_queue_enter(q))
283 ctx = blk_mq_get_ctx(q);
284 hctx = q->mq_ops->map_queue(q, ctx->cpu);
286 rq = __blk_mq_alloc_request(q, hctx, ctx, rw, gfp & ~__GFP_WAIT,
288 if (!rq && (gfp & __GFP_WAIT)) {
289 __blk_mq_run_hw_queue(hctx);
292 ctx = blk_mq_get_ctx(q);
293 hctx = q->mq_ops->map_queue(q, ctx->cpu);
294 rq = __blk_mq_alloc_request(q, hctx, ctx, rw, gfp, reserved);
299 EXPORT_SYMBOL(blk_mq_alloc_request);
301 static void __blk_mq_free_request(struct blk_mq_hw_ctx *hctx,
302 struct blk_mq_ctx *ctx, struct request *rq)
304 const int tag = rq->tag;
305 struct request_queue *q = rq->q;
307 if (rq->cmd_flags & REQ_MQ_INFLIGHT)
308 atomic_dec(&hctx->nr_active);
310 clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
311 blk_mq_put_tag(hctx, tag, &ctx->last_tag);
312 blk_mq_queue_exit(q);
315 void blk_mq_free_request(struct request *rq)
317 struct blk_mq_ctx *ctx = rq->mq_ctx;
318 struct blk_mq_hw_ctx *hctx;
319 struct request_queue *q = rq->q;
321 ctx->rq_completed[rq_is_sync(rq)]++;
323 hctx = q->mq_ops->map_queue(q, ctx->cpu);
324 __blk_mq_free_request(hctx, ctx, rq);
328 * Clone all relevant state from a request that has been put on hold in
329 * the flush state machine into the preallocated flush request that hangs
330 * off the request queue.
332 * For a driver the flush request should be invisible, that's why we are
333 * impersonating the original request here.
335 void blk_mq_clone_flush_request(struct request *flush_rq,
336 struct request *orig_rq)
338 struct blk_mq_hw_ctx *hctx =
339 orig_rq->q->mq_ops->map_queue(orig_rq->q, orig_rq->mq_ctx->cpu);
341 flush_rq->mq_ctx = orig_rq->mq_ctx;
342 flush_rq->tag = orig_rq->tag;
343 memcpy(blk_mq_rq_to_pdu(flush_rq), blk_mq_rq_to_pdu(orig_rq),
347 inline void __blk_mq_end_io(struct request *rq, int error)
349 blk_account_io_done(rq);
352 rq->end_io(rq, error);
354 if (unlikely(blk_bidi_rq(rq)))
355 blk_mq_free_request(rq->next_rq);
356 blk_mq_free_request(rq);
359 EXPORT_SYMBOL(__blk_mq_end_io);
361 void blk_mq_end_io(struct request *rq, int error)
363 if (blk_update_request(rq, error, blk_rq_bytes(rq)))
365 __blk_mq_end_io(rq, error);
367 EXPORT_SYMBOL(blk_mq_end_io);
369 static void __blk_mq_complete_request_remote(void *data)
371 struct request *rq = data;
373 rq->q->softirq_done_fn(rq);
376 void __blk_mq_complete_request(struct request *rq)
378 struct blk_mq_ctx *ctx = rq->mq_ctx;
382 if (!test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags)) {
383 rq->q->softirq_done_fn(rq);
388 if (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags))
389 shared = cpus_share_cache(cpu, ctx->cpu);
391 if (cpu != ctx->cpu && !shared && cpu_online(ctx->cpu)) {
392 rq->csd.func = __blk_mq_complete_request_remote;
395 smp_call_function_single_async(ctx->cpu, &rq->csd);
397 rq->q->softirq_done_fn(rq);
403 * blk_mq_complete_request - end I/O on a request
404 * @rq: the request being processed
407 * Ends all I/O on a request. It does not handle partial completions.
408 * The actual completion happens out-of-order, through a IPI handler.
410 void blk_mq_complete_request(struct request *rq)
412 struct request_queue *q = rq->q;
414 if (unlikely(blk_should_fake_timeout(q)))
416 if (!blk_mark_rq_complete(rq)) {
417 if (q->softirq_done_fn)
418 __blk_mq_complete_request(rq);
420 blk_mq_end_io(rq, rq->errors);
423 EXPORT_SYMBOL(blk_mq_complete_request);
425 static void blk_mq_start_request(struct request *rq, bool last)
427 struct request_queue *q = rq->q;
429 trace_block_rq_issue(q, rq);
431 rq->resid_len = blk_rq_bytes(rq);
432 if (unlikely(blk_bidi_rq(rq)))
433 rq->next_rq->resid_len = blk_rq_bytes(rq->next_rq);
436 * Just mark start time and set the started bit. Due to memory
437 * ordering, we know we'll see the correct deadline as long as
438 * REQ_ATOMIC_STARTED is seen. Use the default queue timeout,
439 * unless one has been set in the request.
442 rq->deadline = jiffies + q->rq_timeout;
444 rq->deadline = jiffies + rq->timeout;
447 * Mark us as started and clear complete. Complete might have been
448 * set if requeue raced with timeout, which then marked it as
449 * complete. So be sure to clear complete again when we start
450 * the request, otherwise we'll ignore the completion event.
452 set_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
453 clear_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags);
455 if (q->dma_drain_size && blk_rq_bytes(rq)) {
457 * Make sure space for the drain appears. We know we can do
458 * this because max_hw_segments has been adjusted to be one
459 * fewer than the device can handle.
461 rq->nr_phys_segments++;
465 * Flag the last request in the series so that drivers know when IO
466 * should be kicked off, if they don't do it on a per-request basis.
468 * Note: the flag isn't the only condition drivers should do kick off.
469 * If drive is busy, the last request might not have the bit set.
472 rq->cmd_flags |= REQ_END;
475 static void __blk_mq_requeue_request(struct request *rq)
477 struct request_queue *q = rq->q;
479 trace_block_rq_requeue(q, rq);
480 clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
482 rq->cmd_flags &= ~REQ_END;
484 if (q->dma_drain_size && blk_rq_bytes(rq))
485 rq->nr_phys_segments--;
488 void blk_mq_requeue_request(struct request *rq)
490 __blk_mq_requeue_request(rq);
491 blk_clear_rq_complete(rq);
493 BUG_ON(blk_queued_rq(rq));
494 blk_mq_add_to_requeue_list(rq, true);
496 EXPORT_SYMBOL(blk_mq_requeue_request);
498 static void blk_mq_requeue_work(struct work_struct *work)
500 struct request_queue *q =
501 container_of(work, struct request_queue, requeue_work);
503 struct request *rq, *next;
506 spin_lock_irqsave(&q->requeue_lock, flags);
507 list_splice_init(&q->requeue_list, &rq_list);
508 spin_unlock_irqrestore(&q->requeue_lock, flags);
510 list_for_each_entry_safe(rq, next, &rq_list, queuelist) {
511 if (!(rq->cmd_flags & REQ_SOFTBARRIER))
514 rq->cmd_flags &= ~REQ_SOFTBARRIER;
515 list_del_init(&rq->queuelist);
516 blk_mq_insert_request(rq, true, false, false);
519 while (!list_empty(&rq_list)) {
520 rq = list_entry(rq_list.next, struct request, queuelist);
521 list_del_init(&rq->queuelist);
522 blk_mq_insert_request(rq, false, false, false);
525 blk_mq_run_queues(q, false);
528 void blk_mq_add_to_requeue_list(struct request *rq, bool at_head)
530 struct request_queue *q = rq->q;
534 * We abuse this flag that is otherwise used by the I/O scheduler to
535 * request head insertation from the workqueue.
537 BUG_ON(rq->cmd_flags & REQ_SOFTBARRIER);
539 spin_lock_irqsave(&q->requeue_lock, flags);
541 rq->cmd_flags |= REQ_SOFTBARRIER;
542 list_add(&rq->queuelist, &q->requeue_list);
544 list_add_tail(&rq->queuelist, &q->requeue_list);
546 spin_unlock_irqrestore(&q->requeue_lock, flags);
548 EXPORT_SYMBOL(blk_mq_add_to_requeue_list);
550 void blk_mq_kick_requeue_list(struct request_queue *q)
552 kblockd_schedule_work(&q->requeue_work);
554 EXPORT_SYMBOL(blk_mq_kick_requeue_list);
556 struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag)
558 return tags->rqs[tag];
560 EXPORT_SYMBOL(blk_mq_tag_to_rq);
562 struct blk_mq_timeout_data {
563 struct blk_mq_hw_ctx *hctx;
565 unsigned int *next_set;
568 static void blk_mq_timeout_check(void *__data, unsigned long *free_tags)
570 struct blk_mq_timeout_data *data = __data;
571 struct blk_mq_hw_ctx *hctx = data->hctx;
574 /* It may not be in flight yet (this is where
575 * the REQ_ATOMIC_STARTED flag comes in). The requests are
576 * statically allocated, so we know it's always safe to access the
577 * memory associated with a bit offset into ->rqs[].
583 tag = find_next_zero_bit(free_tags, hctx->tags->nr_tags, tag);
584 if (tag >= hctx->tags->nr_tags)
587 rq = blk_mq_tag_to_rq(hctx->tags, tag++);
588 if (rq->q != hctx->queue)
590 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
593 blk_rq_check_expired(rq, data->next, data->next_set);
597 static void blk_mq_hw_ctx_check_timeout(struct blk_mq_hw_ctx *hctx,
599 unsigned int *next_set)
601 struct blk_mq_timeout_data data = {
604 .next_set = next_set,
608 * Ask the tagging code to iterate busy requests, so we can
609 * check them for timeout.
611 blk_mq_tag_busy_iter(hctx->tags, blk_mq_timeout_check, &data);
614 static enum blk_eh_timer_return blk_mq_rq_timed_out(struct request *rq)
616 struct request_queue *q = rq->q;
619 * We know that complete is set at this point. If STARTED isn't set
620 * anymore, then the request isn't active and the "timeout" should
621 * just be ignored. This can happen due to the bitflag ordering.
622 * Timeout first checks if STARTED is set, and if it is, assumes
623 * the request is active. But if we race with completion, then
624 * we both flags will get cleared. So check here again, and ignore
625 * a timeout event with a request that isn't active.
627 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
628 return BLK_EH_NOT_HANDLED;
630 if (!q->mq_ops->timeout)
631 return BLK_EH_RESET_TIMER;
633 return q->mq_ops->timeout(rq);
636 static void blk_mq_rq_timer(unsigned long data)
638 struct request_queue *q = (struct request_queue *) data;
639 struct blk_mq_hw_ctx *hctx;
640 unsigned long next = 0;
643 queue_for_each_hw_ctx(q, hctx, i) {
645 * If not software queues are currently mapped to this
646 * hardware queue, there's nothing to check
648 if (!hctx->nr_ctx || !hctx->tags)
651 blk_mq_hw_ctx_check_timeout(hctx, &next, &next_set);
655 next = blk_rq_timeout(round_jiffies_up(next));
656 mod_timer(&q->timeout, next);
658 queue_for_each_hw_ctx(q, hctx, i)
659 blk_mq_tag_idle(hctx);
664 * Reverse check our software queue for entries that we could potentially
665 * merge with. Currently includes a hand-wavy stop count of 8, to not spend
666 * too much time checking for merges.
668 static bool blk_mq_attempt_merge(struct request_queue *q,
669 struct blk_mq_ctx *ctx, struct bio *bio)
674 list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) {
680 if (!blk_rq_merge_ok(rq, bio))
683 el_ret = blk_try_merge(rq, bio);
684 if (el_ret == ELEVATOR_BACK_MERGE) {
685 if (bio_attempt_back_merge(q, rq, bio)) {
690 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
691 if (bio_attempt_front_merge(q, rq, bio)) {
703 * Process software queues that have been marked busy, splicing them
704 * to the for-dispatch
706 static void flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list)
708 struct blk_mq_ctx *ctx;
711 for (i = 0; i < hctx->ctx_map.map_size; i++) {
712 struct blk_align_bitmap *bm = &hctx->ctx_map.map[i];
713 unsigned int off, bit;
719 off = i * hctx->ctx_map.bits_per_word;
721 bit = find_next_bit(&bm->word, bm->depth, bit);
722 if (bit >= bm->depth)
725 ctx = hctx->ctxs[bit + off];
726 clear_bit(bit, &bm->word);
727 spin_lock(&ctx->lock);
728 list_splice_tail_init(&ctx->rq_list, list);
729 spin_unlock(&ctx->lock);
737 * Run this hardware queue, pulling any software queues mapped to it in.
738 * Note that this function currently has various problems around ordering
739 * of IO. In particular, we'd like FIFO behaviour on handling existing
740 * items on the hctx->dispatch list. Ignore that for now.
742 static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
744 struct request_queue *q = hctx->queue;
749 WARN_ON(!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask));
751 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
757 * Touch any software queue that has pending entries.
759 flush_busy_ctxs(hctx, &rq_list);
762 * If we have previous entries on our dispatch list, grab them
763 * and stuff them at the front for more fair dispatch.
765 if (!list_empty_careful(&hctx->dispatch)) {
766 spin_lock(&hctx->lock);
767 if (!list_empty(&hctx->dispatch))
768 list_splice_init(&hctx->dispatch, &rq_list);
769 spin_unlock(&hctx->lock);
773 * Now process all the entries, sending them to the driver.
776 while (!list_empty(&rq_list)) {
779 rq = list_first_entry(&rq_list, struct request, queuelist);
780 list_del_init(&rq->queuelist);
782 blk_mq_start_request(rq, list_empty(&rq_list));
784 ret = q->mq_ops->queue_rq(hctx, rq);
786 case BLK_MQ_RQ_QUEUE_OK:
789 case BLK_MQ_RQ_QUEUE_BUSY:
790 list_add(&rq->queuelist, &rq_list);
791 __blk_mq_requeue_request(rq);
794 pr_err("blk-mq: bad return on queue: %d\n", ret);
795 case BLK_MQ_RQ_QUEUE_ERROR:
797 blk_mq_end_io(rq, rq->errors);
801 if (ret == BLK_MQ_RQ_QUEUE_BUSY)
806 hctx->dispatched[0]++;
807 else if (queued < (1 << (BLK_MQ_MAX_DISPATCH_ORDER - 1)))
808 hctx->dispatched[ilog2(queued) + 1]++;
811 * Any items that need requeuing? Stuff them into hctx->dispatch,
812 * that is where we will continue on next queue run.
814 if (!list_empty(&rq_list)) {
815 spin_lock(&hctx->lock);
816 list_splice(&rq_list, &hctx->dispatch);
817 spin_unlock(&hctx->lock);
822 * It'd be great if the workqueue API had a way to pass
823 * in a mask and had some smarts for more clever placement.
824 * For now we just round-robin here, switching for every
825 * BLK_MQ_CPU_WORK_BATCH queued items.
827 static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx)
829 int cpu = hctx->next_cpu;
831 if (--hctx->next_cpu_batch <= 0) {
834 next_cpu = cpumask_next(hctx->next_cpu, hctx->cpumask);
835 if (next_cpu >= nr_cpu_ids)
836 next_cpu = cpumask_first(hctx->cpumask);
838 hctx->next_cpu = next_cpu;
839 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
845 void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
847 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
850 if (!async && cpumask_test_cpu(smp_processor_id(), hctx->cpumask))
851 __blk_mq_run_hw_queue(hctx);
852 else if (hctx->queue->nr_hw_queues == 1)
853 kblockd_schedule_delayed_work(&hctx->run_work, 0);
857 cpu = blk_mq_hctx_next_cpu(hctx);
858 kblockd_schedule_delayed_work_on(cpu, &hctx->run_work, 0);
862 void blk_mq_run_queues(struct request_queue *q, bool async)
864 struct blk_mq_hw_ctx *hctx;
867 queue_for_each_hw_ctx(q, hctx, i) {
868 if ((!blk_mq_hctx_has_pending(hctx) &&
869 list_empty_careful(&hctx->dispatch)) ||
870 test_bit(BLK_MQ_S_STOPPED, &hctx->state))
874 blk_mq_run_hw_queue(hctx, async);
878 EXPORT_SYMBOL(blk_mq_run_queues);
880 void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
882 cancel_delayed_work(&hctx->run_work);
883 cancel_delayed_work(&hctx->delay_work);
884 set_bit(BLK_MQ_S_STOPPED, &hctx->state);
886 EXPORT_SYMBOL(blk_mq_stop_hw_queue);
888 void blk_mq_stop_hw_queues(struct request_queue *q)
890 struct blk_mq_hw_ctx *hctx;
893 queue_for_each_hw_ctx(q, hctx, i)
894 blk_mq_stop_hw_queue(hctx);
896 EXPORT_SYMBOL(blk_mq_stop_hw_queues);
898 void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
900 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
903 __blk_mq_run_hw_queue(hctx);
906 EXPORT_SYMBOL(blk_mq_start_hw_queue);
908 void blk_mq_start_hw_queues(struct request_queue *q)
910 struct blk_mq_hw_ctx *hctx;
913 queue_for_each_hw_ctx(q, hctx, i)
914 blk_mq_start_hw_queue(hctx);
916 EXPORT_SYMBOL(blk_mq_start_hw_queues);
919 void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async)
921 struct blk_mq_hw_ctx *hctx;
924 queue_for_each_hw_ctx(q, hctx, i) {
925 if (!test_bit(BLK_MQ_S_STOPPED, &hctx->state))
928 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
930 blk_mq_run_hw_queue(hctx, async);
934 EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
936 static void blk_mq_run_work_fn(struct work_struct *work)
938 struct blk_mq_hw_ctx *hctx;
940 hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work);
942 __blk_mq_run_hw_queue(hctx);
945 static void blk_mq_delay_work_fn(struct work_struct *work)
947 struct blk_mq_hw_ctx *hctx;
949 hctx = container_of(work, struct blk_mq_hw_ctx, delay_work.work);
951 if (test_and_clear_bit(BLK_MQ_S_STOPPED, &hctx->state))
952 __blk_mq_run_hw_queue(hctx);
955 void blk_mq_delay_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
957 unsigned long tmo = msecs_to_jiffies(msecs);
959 if (hctx->queue->nr_hw_queues == 1)
960 kblockd_schedule_delayed_work(&hctx->delay_work, tmo);
964 cpu = blk_mq_hctx_next_cpu(hctx);
965 kblockd_schedule_delayed_work_on(cpu, &hctx->delay_work, tmo);
968 EXPORT_SYMBOL(blk_mq_delay_queue);
970 static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx,
971 struct request *rq, bool at_head)
973 struct blk_mq_ctx *ctx = rq->mq_ctx;
975 trace_block_rq_insert(hctx->queue, rq);
978 list_add(&rq->queuelist, &ctx->rq_list);
980 list_add_tail(&rq->queuelist, &ctx->rq_list);
982 blk_mq_hctx_mark_pending(hctx, ctx);
985 * We do this early, to ensure we are on the right CPU.
990 void blk_mq_insert_request(struct request *rq, bool at_head, bool run_queue,
993 struct request_queue *q = rq->q;
994 struct blk_mq_hw_ctx *hctx;
995 struct blk_mq_ctx *ctx = rq->mq_ctx, *current_ctx;
997 current_ctx = blk_mq_get_ctx(q);
998 if (!cpu_online(ctx->cpu))
999 rq->mq_ctx = ctx = current_ctx;
1001 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1003 if (rq->cmd_flags & (REQ_FLUSH | REQ_FUA) &&
1004 !(rq->cmd_flags & (REQ_FLUSH_SEQ))) {
1005 blk_insert_flush(rq);
1007 spin_lock(&ctx->lock);
1008 __blk_mq_insert_request(hctx, rq, at_head);
1009 spin_unlock(&ctx->lock);
1013 blk_mq_run_hw_queue(hctx, async);
1015 blk_mq_put_ctx(current_ctx);
1018 static void blk_mq_insert_requests(struct request_queue *q,
1019 struct blk_mq_ctx *ctx,
1020 struct list_head *list,
1025 struct blk_mq_hw_ctx *hctx;
1026 struct blk_mq_ctx *current_ctx;
1028 trace_block_unplug(q, depth, !from_schedule);
1030 current_ctx = blk_mq_get_ctx(q);
1032 if (!cpu_online(ctx->cpu))
1034 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1037 * preemption doesn't flush plug list, so it's possible ctx->cpu is
1040 spin_lock(&ctx->lock);
1041 while (!list_empty(list)) {
1044 rq = list_first_entry(list, struct request, queuelist);
1045 list_del_init(&rq->queuelist);
1047 __blk_mq_insert_request(hctx, rq, false);
1049 spin_unlock(&ctx->lock);
1051 blk_mq_run_hw_queue(hctx, from_schedule);
1052 blk_mq_put_ctx(current_ctx);
1055 static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b)
1057 struct request *rqa = container_of(a, struct request, queuelist);
1058 struct request *rqb = container_of(b, struct request, queuelist);
1060 return !(rqa->mq_ctx < rqb->mq_ctx ||
1061 (rqa->mq_ctx == rqb->mq_ctx &&
1062 blk_rq_pos(rqa) < blk_rq_pos(rqb)));
1065 void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
1067 struct blk_mq_ctx *this_ctx;
1068 struct request_queue *this_q;
1071 LIST_HEAD(ctx_list);
1074 list_splice_init(&plug->mq_list, &list);
1076 list_sort(NULL, &list, plug_ctx_cmp);
1082 while (!list_empty(&list)) {
1083 rq = list_entry_rq(list.next);
1084 list_del_init(&rq->queuelist);
1086 if (rq->mq_ctx != this_ctx) {
1088 blk_mq_insert_requests(this_q, this_ctx,
1093 this_ctx = rq->mq_ctx;
1099 list_add_tail(&rq->queuelist, &ctx_list);
1103 * If 'this_ctx' is set, we know we have entries to complete
1104 * on 'ctx_list'. Do those.
1107 blk_mq_insert_requests(this_q, this_ctx, &ctx_list, depth,
1112 static void blk_mq_bio_to_request(struct request *rq, struct bio *bio)
1114 init_request_from_bio(rq, bio);
1115 blk_account_io_start(rq, 1);
1118 static inline bool blk_mq_merge_queue_io(struct blk_mq_hw_ctx *hctx,
1119 struct blk_mq_ctx *ctx,
1120 struct request *rq, struct bio *bio)
1122 struct request_queue *q = hctx->queue;
1124 if (!(hctx->flags & BLK_MQ_F_SHOULD_MERGE)) {
1125 blk_mq_bio_to_request(rq, bio);
1126 spin_lock(&ctx->lock);
1128 __blk_mq_insert_request(hctx, rq, false);
1129 spin_unlock(&ctx->lock);
1132 spin_lock(&ctx->lock);
1133 if (!blk_mq_attempt_merge(q, ctx, bio)) {
1134 blk_mq_bio_to_request(rq, bio);
1138 spin_unlock(&ctx->lock);
1139 __blk_mq_free_request(hctx, ctx, rq);
1144 struct blk_map_ctx {
1145 struct blk_mq_hw_ctx *hctx;
1146 struct blk_mq_ctx *ctx;
1149 static struct request *blk_mq_map_request(struct request_queue *q,
1151 struct blk_map_ctx *data)
1153 struct blk_mq_hw_ctx *hctx;
1154 struct blk_mq_ctx *ctx;
1156 int rw = bio_data_dir(bio);
1158 if (unlikely(blk_mq_queue_enter(q))) {
1159 bio_endio(bio, -EIO);
1163 ctx = blk_mq_get_ctx(q);
1164 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1166 if (rw_is_sync(bio->bi_rw))
1169 trace_block_getrq(q, bio, rw);
1170 rq = __blk_mq_alloc_request(q, hctx, ctx, rw, GFP_ATOMIC, false);
1171 if (unlikely(!rq)) {
1172 __blk_mq_run_hw_queue(hctx);
1173 blk_mq_put_ctx(ctx);
1174 trace_block_sleeprq(q, bio, rw);
1176 ctx = blk_mq_get_ctx(q);
1177 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1178 rq = __blk_mq_alloc_request(q, hctx, ctx, rw,
1179 __GFP_WAIT|GFP_ATOMIC, false);
1189 * Multiple hardware queue variant. This will not use per-process plugs,
1190 * but will attempt to bypass the hctx queueing if we can go straight to
1191 * hardware for SYNC IO.
1193 static void blk_mq_make_request(struct request_queue *q, struct bio *bio)
1195 const int is_sync = rw_is_sync(bio->bi_rw);
1196 const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
1197 struct blk_map_ctx data;
1200 blk_queue_bounce(q, &bio);
1202 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
1203 bio_endio(bio, -EIO);
1207 rq = blk_mq_map_request(q, bio, &data);
1211 if (unlikely(is_flush_fua)) {
1212 blk_mq_bio_to_request(rq, bio);
1213 blk_insert_flush(rq);
1220 blk_mq_bio_to_request(rq, bio);
1221 blk_mq_start_request(rq, true);
1224 * For OK queue, we are done. For error, kill it. Any other
1225 * error (busy), just add it to our list as we previously
1228 ret = q->mq_ops->queue_rq(data.hctx, rq);
1229 if (ret == BLK_MQ_RQ_QUEUE_OK)
1232 __blk_mq_requeue_request(rq);
1234 if (ret == BLK_MQ_RQ_QUEUE_ERROR) {
1236 blk_mq_end_io(rq, rq->errors);
1242 if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) {
1244 * For a SYNC request, send it to the hardware immediately. For
1245 * an ASYNC request, just ensure that we run it later on. The
1246 * latter allows for merging opportunities and more efficient
1250 blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua);
1253 blk_mq_put_ctx(data.ctx);
1257 * Single hardware queue variant. This will attempt to use any per-process
1258 * plug for merging and IO deferral.
1260 static void blk_sq_make_request(struct request_queue *q, struct bio *bio)
1262 const int is_sync = rw_is_sync(bio->bi_rw);
1263 const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
1264 unsigned int use_plug, request_count = 0;
1265 struct blk_map_ctx data;
1269 * If we have multiple hardware queues, just go directly to
1270 * one of those for sync IO.
1272 use_plug = !is_flush_fua && !is_sync;
1274 blk_queue_bounce(q, &bio);
1276 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
1277 bio_endio(bio, -EIO);
1281 if (use_plug && !blk_queue_nomerges(q) &&
1282 blk_attempt_plug_merge(q, bio, &request_count))
1285 rq = blk_mq_map_request(q, bio, &data);
1287 if (unlikely(is_flush_fua)) {
1288 blk_mq_bio_to_request(rq, bio);
1289 blk_insert_flush(rq);
1294 * A task plug currently exists. Since this is completely lockless,
1295 * utilize that to temporarily store requests until the task is
1296 * either done or scheduled away.
1299 struct blk_plug *plug = current->plug;
1302 blk_mq_bio_to_request(rq, bio);
1303 if (list_empty(&plug->mq_list))
1304 trace_block_plug(q);
1305 else if (request_count >= BLK_MAX_REQUEST_COUNT) {
1306 blk_flush_plug_list(plug, false);
1307 trace_block_plug(q);
1309 list_add_tail(&rq->queuelist, &plug->mq_list);
1310 blk_mq_put_ctx(data.ctx);
1315 if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) {
1317 * For a SYNC request, send it to the hardware immediately. For
1318 * an ASYNC request, just ensure that we run it later on. The
1319 * latter allows for merging opportunities and more efficient
1323 blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua);
1326 blk_mq_put_ctx(data.ctx);
1330 * Default mapping to a software queue, since we use one per CPU.
1332 struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q, const int cpu)
1334 return q->queue_hw_ctx[q->mq_map[cpu]];
1336 EXPORT_SYMBOL(blk_mq_map_queue);
1338 static void blk_mq_free_rq_map(struct blk_mq_tag_set *set,
1339 struct blk_mq_tags *tags, unsigned int hctx_idx)
1343 if (tags->rqs && set->ops->exit_request) {
1346 for (i = 0; i < tags->nr_tags; i++) {
1349 set->ops->exit_request(set->driver_data, tags->rqs[i],
1354 while (!list_empty(&tags->page_list)) {
1355 page = list_first_entry(&tags->page_list, struct page, lru);
1356 list_del_init(&page->lru);
1357 __free_pages(page, page->private);
1362 blk_mq_free_tags(tags);
1365 static size_t order_to_size(unsigned int order)
1367 return (size_t)PAGE_SIZE << order;
1370 static struct blk_mq_tags *blk_mq_init_rq_map(struct blk_mq_tag_set *set,
1371 unsigned int hctx_idx)
1373 struct blk_mq_tags *tags;
1374 unsigned int i, j, entries_per_page, max_order = 4;
1375 size_t rq_size, left;
1377 tags = blk_mq_init_tags(set->queue_depth, set->reserved_tags,
1382 INIT_LIST_HEAD(&tags->page_list);
1384 tags->rqs = kmalloc_node(set->queue_depth * sizeof(struct request *),
1385 GFP_KERNEL, set->numa_node);
1387 blk_mq_free_tags(tags);
1392 * rq_size is the size of the request plus driver payload, rounded
1393 * to the cacheline size
1395 rq_size = round_up(sizeof(struct request) + set->cmd_size,
1397 left = rq_size * set->queue_depth;
1399 for (i = 0; i < set->queue_depth; ) {
1400 int this_order = max_order;
1405 while (left < order_to_size(this_order - 1) && this_order)
1409 page = alloc_pages_node(set->numa_node, GFP_KERNEL,
1415 if (order_to_size(this_order) < rq_size)
1422 page->private = this_order;
1423 list_add_tail(&page->lru, &tags->page_list);
1425 p = page_address(page);
1426 entries_per_page = order_to_size(this_order) / rq_size;
1427 to_do = min(entries_per_page, set->queue_depth - i);
1428 left -= to_do * rq_size;
1429 for (j = 0; j < to_do; j++) {
1431 if (set->ops->init_request) {
1432 if (set->ops->init_request(set->driver_data,
1433 tags->rqs[i], hctx_idx, i,
1446 pr_warn("%s: failed to allocate requests\n", __func__);
1447 blk_mq_free_rq_map(set, tags, hctx_idx);
1451 static void blk_mq_free_bitmap(struct blk_mq_ctxmap *bitmap)
1456 static int blk_mq_alloc_bitmap(struct blk_mq_ctxmap *bitmap, int node)
1458 unsigned int bpw = 8, total, num_maps, i;
1460 bitmap->bits_per_word = bpw;
1462 num_maps = ALIGN(nr_cpu_ids, bpw) / bpw;
1463 bitmap->map = kzalloc_node(num_maps * sizeof(struct blk_align_bitmap),
1468 bitmap->map_size = num_maps;
1471 for (i = 0; i < num_maps; i++) {
1472 bitmap->map[i].depth = min(total, bitmap->bits_per_word);
1473 total -= bitmap->map[i].depth;
1479 static int blk_mq_hctx_cpu_offline(struct blk_mq_hw_ctx *hctx, int cpu)
1481 struct request_queue *q = hctx->queue;
1482 struct blk_mq_ctx *ctx;
1486 * Move ctx entries to new CPU, if this one is going away.
1488 ctx = __blk_mq_get_ctx(q, cpu);
1490 spin_lock(&ctx->lock);
1491 if (!list_empty(&ctx->rq_list)) {
1492 list_splice_init(&ctx->rq_list, &tmp);
1493 blk_mq_hctx_clear_pending(hctx, ctx);
1495 spin_unlock(&ctx->lock);
1497 if (list_empty(&tmp))
1500 ctx = blk_mq_get_ctx(q);
1501 spin_lock(&ctx->lock);
1503 while (!list_empty(&tmp)) {
1506 rq = list_first_entry(&tmp, struct request, queuelist);
1508 list_move_tail(&rq->queuelist, &ctx->rq_list);
1511 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1512 blk_mq_hctx_mark_pending(hctx, ctx);
1514 spin_unlock(&ctx->lock);
1516 blk_mq_run_hw_queue(hctx, true);
1517 blk_mq_put_ctx(ctx);
1521 static int blk_mq_hctx_cpu_online(struct blk_mq_hw_ctx *hctx, int cpu)
1523 struct request_queue *q = hctx->queue;
1524 struct blk_mq_tag_set *set = q->tag_set;
1526 if (set->tags[hctx->queue_num])
1529 set->tags[hctx->queue_num] = blk_mq_init_rq_map(set, hctx->queue_num);
1530 if (!set->tags[hctx->queue_num])
1533 hctx->tags = set->tags[hctx->queue_num];
1537 static int blk_mq_hctx_notify(void *data, unsigned long action,
1540 struct blk_mq_hw_ctx *hctx = data;
1542 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN)
1543 return blk_mq_hctx_cpu_offline(hctx, cpu);
1544 else if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN)
1545 return blk_mq_hctx_cpu_online(hctx, cpu);
1550 static void blk_mq_exit_hw_queues(struct request_queue *q,
1551 struct blk_mq_tag_set *set, int nr_queue)
1553 struct blk_mq_hw_ctx *hctx;
1556 queue_for_each_hw_ctx(q, hctx, i) {
1560 if (set->ops->exit_hctx)
1561 set->ops->exit_hctx(hctx, i);
1563 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
1565 blk_mq_free_bitmap(&hctx->ctx_map);
1570 static void blk_mq_free_hw_queues(struct request_queue *q,
1571 struct blk_mq_tag_set *set)
1573 struct blk_mq_hw_ctx *hctx;
1576 queue_for_each_hw_ctx(q, hctx, i) {
1577 free_cpumask_var(hctx->cpumask);
1582 static int blk_mq_init_hw_queues(struct request_queue *q,
1583 struct blk_mq_tag_set *set)
1585 struct blk_mq_hw_ctx *hctx;
1589 * Initialize hardware queues
1591 queue_for_each_hw_ctx(q, hctx, i) {
1594 node = hctx->numa_node;
1595 if (node == NUMA_NO_NODE)
1596 node = hctx->numa_node = set->numa_node;
1598 INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn);
1599 INIT_DELAYED_WORK(&hctx->delay_work, blk_mq_delay_work_fn);
1600 spin_lock_init(&hctx->lock);
1601 INIT_LIST_HEAD(&hctx->dispatch);
1603 hctx->queue_num = i;
1604 hctx->flags = set->flags;
1605 hctx->cmd_size = set->cmd_size;
1607 blk_mq_init_cpu_notifier(&hctx->cpu_notifier,
1608 blk_mq_hctx_notify, hctx);
1609 blk_mq_register_cpu_notifier(&hctx->cpu_notifier);
1611 hctx->tags = set->tags[i];
1614 * Allocate space for all possible cpus to avoid allocation in
1617 hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *),
1622 if (blk_mq_alloc_bitmap(&hctx->ctx_map, node))
1627 if (set->ops->init_hctx &&
1628 set->ops->init_hctx(hctx, set->driver_data, i))
1632 if (i == q->nr_hw_queues)
1638 blk_mq_exit_hw_queues(q, set, i);
1643 static void blk_mq_init_cpu_queues(struct request_queue *q,
1644 unsigned int nr_hw_queues)
1648 for_each_possible_cpu(i) {
1649 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
1650 struct blk_mq_hw_ctx *hctx;
1652 memset(__ctx, 0, sizeof(*__ctx));
1654 spin_lock_init(&__ctx->lock);
1655 INIT_LIST_HEAD(&__ctx->rq_list);
1658 /* If the cpu isn't online, the cpu is mapped to first hctx */
1662 hctx = q->mq_ops->map_queue(q, i);
1663 cpumask_set_cpu(i, hctx->cpumask);
1667 * Set local node, IFF we have more than one hw queue. If
1668 * not, we remain on the home node of the device
1670 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
1671 hctx->numa_node = cpu_to_node(i);
1675 static void blk_mq_map_swqueue(struct request_queue *q)
1678 struct blk_mq_hw_ctx *hctx;
1679 struct blk_mq_ctx *ctx;
1681 queue_for_each_hw_ctx(q, hctx, i) {
1682 cpumask_clear(hctx->cpumask);
1687 * Map software to hardware queues
1689 queue_for_each_ctx(q, ctx, i) {
1690 /* If the cpu isn't online, the cpu is mapped to first hctx */
1694 hctx = q->mq_ops->map_queue(q, i);
1695 cpumask_set_cpu(i, hctx->cpumask);
1696 ctx->index_hw = hctx->nr_ctx;
1697 hctx->ctxs[hctx->nr_ctx++] = ctx;
1700 queue_for_each_hw_ctx(q, hctx, i) {
1702 * If not software queues are mapped to this hardware queue,
1703 * disable it and free the request entries
1705 if (!hctx->nr_ctx) {
1706 struct blk_mq_tag_set *set = q->tag_set;
1709 blk_mq_free_rq_map(set, set->tags[i], i);
1710 set->tags[i] = NULL;
1717 * Initialize batch roundrobin counts
1719 hctx->next_cpu = cpumask_first(hctx->cpumask);
1720 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
1724 static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set *set)
1726 struct blk_mq_hw_ctx *hctx;
1727 struct request_queue *q;
1731 if (set->tag_list.next == set->tag_list.prev)
1736 list_for_each_entry(q, &set->tag_list, tag_set_list) {
1737 blk_mq_freeze_queue(q);
1739 queue_for_each_hw_ctx(q, hctx, i) {
1741 hctx->flags |= BLK_MQ_F_TAG_SHARED;
1743 hctx->flags &= ~BLK_MQ_F_TAG_SHARED;
1745 blk_mq_unfreeze_queue(q);
1749 static void blk_mq_del_queue_tag_set(struct request_queue *q)
1751 struct blk_mq_tag_set *set = q->tag_set;
1753 blk_mq_freeze_queue(q);
1755 mutex_lock(&set->tag_list_lock);
1756 list_del_init(&q->tag_set_list);
1757 blk_mq_update_tag_set_depth(set);
1758 mutex_unlock(&set->tag_list_lock);
1760 blk_mq_unfreeze_queue(q);
1763 static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set,
1764 struct request_queue *q)
1768 mutex_lock(&set->tag_list_lock);
1769 list_add_tail(&q->tag_set_list, &set->tag_list);
1770 blk_mq_update_tag_set_depth(set);
1771 mutex_unlock(&set->tag_list_lock);
1774 struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
1776 struct blk_mq_hw_ctx **hctxs;
1777 struct blk_mq_ctx *ctx;
1778 struct request_queue *q;
1782 ctx = alloc_percpu(struct blk_mq_ctx);
1784 return ERR_PTR(-ENOMEM);
1786 hctxs = kmalloc_node(set->nr_hw_queues * sizeof(*hctxs), GFP_KERNEL,
1792 map = blk_mq_make_queue_map(set);
1796 for (i = 0; i < set->nr_hw_queues; i++) {
1797 int node = blk_mq_hw_queue_to_node(map, i);
1799 hctxs[i] = kzalloc_node(sizeof(struct blk_mq_hw_ctx),
1804 if (!zalloc_cpumask_var(&hctxs[i]->cpumask, GFP_KERNEL))
1807 atomic_set(&hctxs[i]->nr_active, 0);
1808 hctxs[i]->numa_node = node;
1809 hctxs[i]->queue_num = i;
1812 q = blk_alloc_queue_node(GFP_KERNEL, set->numa_node);
1816 if (percpu_counter_init(&q->mq_usage_counter, 0))
1819 setup_timer(&q->timeout, blk_mq_rq_timer, (unsigned long) q);
1820 blk_queue_rq_timeout(q, 30000);
1822 q->nr_queues = nr_cpu_ids;
1823 q->nr_hw_queues = set->nr_hw_queues;
1827 q->queue_hw_ctx = hctxs;
1829 q->mq_ops = set->ops;
1830 q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
1832 q->sg_reserved_size = INT_MAX;
1834 INIT_WORK(&q->requeue_work, blk_mq_requeue_work);
1835 INIT_LIST_HEAD(&q->requeue_list);
1836 spin_lock_init(&q->requeue_lock);
1838 if (q->nr_hw_queues > 1)
1839 blk_queue_make_request(q, blk_mq_make_request);
1841 blk_queue_make_request(q, blk_sq_make_request);
1843 blk_queue_rq_timed_out(q, blk_mq_rq_timed_out);
1845 blk_queue_rq_timeout(q, set->timeout);
1848 * Do this after blk_queue_make_request() overrides it...
1850 q->nr_requests = set->queue_depth;
1852 if (set->ops->complete)
1853 blk_queue_softirq_done(q, set->ops->complete);
1855 blk_mq_init_flush(q);
1856 blk_mq_init_cpu_queues(q, set->nr_hw_queues);
1858 q->flush_rq = kzalloc(round_up(sizeof(struct request) +
1859 set->cmd_size, cache_line_size()),
1864 if (blk_mq_init_hw_queues(q, set))
1867 mutex_lock(&all_q_mutex);
1868 list_add_tail(&q->all_q_node, &all_q_list);
1869 mutex_unlock(&all_q_mutex);
1871 blk_mq_add_queue_tag_set(set, q);
1873 blk_mq_map_swqueue(q);
1880 blk_cleanup_queue(q);
1883 for (i = 0; i < set->nr_hw_queues; i++) {
1886 free_cpumask_var(hctxs[i]->cpumask);
1893 return ERR_PTR(-ENOMEM);
1895 EXPORT_SYMBOL(blk_mq_init_queue);
1897 void blk_mq_free_queue(struct request_queue *q)
1899 struct blk_mq_tag_set *set = q->tag_set;
1901 blk_mq_del_queue_tag_set(q);
1903 blk_mq_exit_hw_queues(q, set, set->nr_hw_queues);
1904 blk_mq_free_hw_queues(q, set);
1906 percpu_counter_destroy(&q->mq_usage_counter);
1908 free_percpu(q->queue_ctx);
1909 kfree(q->queue_hw_ctx);
1912 q->queue_ctx = NULL;
1913 q->queue_hw_ctx = NULL;
1916 mutex_lock(&all_q_mutex);
1917 list_del_init(&q->all_q_node);
1918 mutex_unlock(&all_q_mutex);
1921 /* Basically redo blk_mq_init_queue with queue frozen */
1922 static void blk_mq_queue_reinit(struct request_queue *q)
1924 blk_mq_freeze_queue(q);
1926 blk_mq_update_queue_map(q->mq_map, q->nr_hw_queues);
1929 * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
1930 * we should change hctx numa_node according to new topology (this
1931 * involves free and re-allocate memory, worthy doing?)
1934 blk_mq_map_swqueue(q);
1936 blk_mq_unfreeze_queue(q);
1939 static int blk_mq_queue_reinit_notify(struct notifier_block *nb,
1940 unsigned long action, void *hcpu)
1942 struct request_queue *q;
1945 * Before new mappings are established, hotadded cpu might already
1946 * start handling requests. This doesn't break anything as we map
1947 * offline CPUs to first hardware queue. We will re-init the queue
1948 * below to get optimal settings.
1950 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN &&
1951 action != CPU_ONLINE && action != CPU_ONLINE_FROZEN)
1954 mutex_lock(&all_q_mutex);
1955 list_for_each_entry(q, &all_q_list, all_q_node)
1956 blk_mq_queue_reinit(q);
1957 mutex_unlock(&all_q_mutex);
1961 int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
1965 if (!set->nr_hw_queues)
1967 if (!set->queue_depth || set->queue_depth > BLK_MQ_MAX_DEPTH)
1969 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN)
1972 if (!set->nr_hw_queues || !set->ops->queue_rq || !set->ops->map_queue)
1976 set->tags = kmalloc_node(set->nr_hw_queues *
1977 sizeof(struct blk_mq_tags *),
1978 GFP_KERNEL, set->numa_node);
1982 for (i = 0; i < set->nr_hw_queues; i++) {
1983 set->tags[i] = blk_mq_init_rq_map(set, i);
1988 mutex_init(&set->tag_list_lock);
1989 INIT_LIST_HEAD(&set->tag_list);
1995 blk_mq_free_rq_map(set, set->tags[i], i);
1999 EXPORT_SYMBOL(blk_mq_alloc_tag_set);
2001 void blk_mq_free_tag_set(struct blk_mq_tag_set *set)
2005 for (i = 0; i < set->nr_hw_queues; i++) {
2007 blk_mq_free_rq_map(set, set->tags[i], i);
2012 EXPORT_SYMBOL(blk_mq_free_tag_set);
2014 int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr)
2016 struct blk_mq_tag_set *set = q->tag_set;
2017 struct blk_mq_hw_ctx *hctx;
2020 if (!set || nr > set->queue_depth)
2024 queue_for_each_hw_ctx(q, hctx, i) {
2025 ret = blk_mq_tag_update_depth(hctx->tags, nr);
2031 q->nr_requests = nr;
2036 void blk_mq_disable_hotplug(void)
2038 mutex_lock(&all_q_mutex);
2041 void blk_mq_enable_hotplug(void)
2043 mutex_unlock(&all_q_mutex);
2046 static int __init blk_mq_init(void)
2050 /* Must be called after percpu_counter_hotcpu_callback() */
2051 hotcpu_notifier(blk_mq_queue_reinit_notify, -10);
2055 subsys_initcall(blk_mq_init);