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);
86 /* we have problems freezing the queue if it's initializing */
87 if (!blk_queue_dying(q) &&
88 (!blk_queue_bypass(q) || !blk_queue_init_done(q)))
91 __percpu_counter_add(&q->mq_usage_counter, -1, 1000000);
93 spin_lock_irq(q->queue_lock);
94 ret = wait_event_interruptible_lock_irq(q->mq_freeze_wq,
95 !blk_queue_bypass(q) || blk_queue_dying(q),
97 /* inc usage with lock hold to avoid freeze_queue runs here */
98 if (!ret && !blk_queue_dying(q))
99 __percpu_counter_add(&q->mq_usage_counter, 1, 1000000);
100 else if (blk_queue_dying(q))
102 spin_unlock_irq(q->queue_lock);
107 static void blk_mq_queue_exit(struct request_queue *q)
109 __percpu_counter_add(&q->mq_usage_counter, -1, 1000000);
112 static void __blk_mq_drain_queue(struct request_queue *q)
117 spin_lock_irq(q->queue_lock);
118 count = percpu_counter_sum(&q->mq_usage_counter);
119 spin_unlock_irq(q->queue_lock);
123 blk_mq_run_queues(q, false);
129 * Guarantee no request is in use, so we can change any data structure of
130 * the queue afterward.
132 static void blk_mq_freeze_queue(struct request_queue *q)
136 spin_lock_irq(q->queue_lock);
137 drain = !q->bypass_depth++;
138 queue_flag_set(QUEUE_FLAG_BYPASS, q);
139 spin_unlock_irq(q->queue_lock);
142 __blk_mq_drain_queue(q);
145 void blk_mq_drain_queue(struct request_queue *q)
147 __blk_mq_drain_queue(q);
150 static void blk_mq_unfreeze_queue(struct request_queue *q)
154 spin_lock_irq(q->queue_lock);
155 if (!--q->bypass_depth) {
156 queue_flag_clear(QUEUE_FLAG_BYPASS, q);
159 WARN_ON_ONCE(q->bypass_depth < 0);
160 spin_unlock_irq(q->queue_lock);
162 wake_up_all(&q->mq_freeze_wq);
165 bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx)
167 return blk_mq_has_free_tags(hctx->tags);
169 EXPORT_SYMBOL(blk_mq_can_queue);
171 static void blk_mq_rq_ctx_init(struct request_queue *q, struct blk_mq_ctx *ctx,
172 struct request *rq, unsigned int rw_flags)
174 if (blk_queue_io_stat(q))
175 rw_flags |= REQ_IO_STAT;
177 INIT_LIST_HEAD(&rq->queuelist);
178 /* csd/requeue_work/fifo_time is initialized before use */
181 rq->cmd_flags |= rw_flags;
182 /* do not touch atomic flags, it needs atomic ops against the timer */
184 INIT_HLIST_NODE(&rq->hash);
185 RB_CLEAR_NODE(&rq->rb_node);
188 #ifdef CONFIG_BLK_CGROUP
190 set_start_time_ns(rq);
191 rq->io_start_time_ns = 0;
193 rq->nr_phys_segments = 0;
194 #if defined(CONFIG_BLK_DEV_INTEGRITY)
195 rq->nr_integrity_segments = 0;
198 /* tag was already set */
206 INIT_LIST_HEAD(&rq->timeout_list);
210 rq->end_io_data = NULL;
213 ctx->rq_dispatched[rw_is_sync(rw_flags)]++;
216 static struct request *
217 __blk_mq_alloc_request(struct blk_mq_alloc_data *data, int rw)
222 tag = blk_mq_get_tag(data);
223 if (tag != BLK_MQ_TAG_FAIL) {
224 rq = data->hctx->tags->rqs[tag];
227 if (blk_mq_tag_busy(data->hctx)) {
228 rq->cmd_flags = REQ_MQ_INFLIGHT;
229 atomic_inc(&data->hctx->nr_active);
233 blk_mq_rq_ctx_init(data->q, data->ctx, rq, rw);
240 struct request *blk_mq_alloc_request(struct request_queue *q, int rw, gfp_t gfp,
243 struct blk_mq_ctx *ctx;
244 struct blk_mq_hw_ctx *hctx;
246 struct blk_mq_alloc_data alloc_data;
248 if (blk_mq_queue_enter(q))
251 ctx = blk_mq_get_ctx(q);
252 hctx = q->mq_ops->map_queue(q, ctx->cpu);
253 blk_mq_set_alloc_data(&alloc_data, q, gfp & ~__GFP_WAIT,
254 reserved, ctx, hctx);
256 rq = __blk_mq_alloc_request(&alloc_data, rw);
257 if (!rq && (gfp & __GFP_WAIT)) {
258 __blk_mq_run_hw_queue(hctx);
261 ctx = blk_mq_get_ctx(q);
262 hctx = q->mq_ops->map_queue(q, ctx->cpu);
263 blk_mq_set_alloc_data(&alloc_data, q, gfp, reserved, ctx,
265 rq = __blk_mq_alloc_request(&alloc_data, rw);
266 ctx = alloc_data.ctx;
271 EXPORT_SYMBOL(blk_mq_alloc_request);
273 static void __blk_mq_free_request(struct blk_mq_hw_ctx *hctx,
274 struct blk_mq_ctx *ctx, struct request *rq)
276 const int tag = rq->tag;
277 struct request_queue *q = rq->q;
279 if (rq->cmd_flags & REQ_MQ_INFLIGHT)
280 atomic_dec(&hctx->nr_active);
282 clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
283 blk_mq_put_tag(hctx, tag, &ctx->last_tag);
284 blk_mq_queue_exit(q);
287 void blk_mq_free_request(struct request *rq)
289 struct blk_mq_ctx *ctx = rq->mq_ctx;
290 struct blk_mq_hw_ctx *hctx;
291 struct request_queue *q = rq->q;
293 ctx->rq_completed[rq_is_sync(rq)]++;
295 hctx = q->mq_ops->map_queue(q, ctx->cpu);
296 __blk_mq_free_request(hctx, ctx, rq);
300 * Clone all relevant state from a request that has been put on hold in
301 * the flush state machine into the preallocated flush request that hangs
302 * off the request queue.
304 * For a driver the flush request should be invisible, that's why we are
305 * impersonating the original request here.
307 void blk_mq_clone_flush_request(struct request *flush_rq,
308 struct request *orig_rq)
310 struct blk_mq_hw_ctx *hctx =
311 orig_rq->q->mq_ops->map_queue(orig_rq->q, orig_rq->mq_ctx->cpu);
313 flush_rq->mq_ctx = orig_rq->mq_ctx;
314 flush_rq->tag = orig_rq->tag;
315 memcpy(blk_mq_rq_to_pdu(flush_rq), blk_mq_rq_to_pdu(orig_rq),
319 inline void __blk_mq_end_io(struct request *rq, int error)
321 blk_account_io_done(rq);
324 rq->end_io(rq, error);
326 if (unlikely(blk_bidi_rq(rq)))
327 blk_mq_free_request(rq->next_rq);
328 blk_mq_free_request(rq);
331 EXPORT_SYMBOL(__blk_mq_end_io);
333 void blk_mq_end_io(struct request *rq, int error)
335 if (blk_update_request(rq, error, blk_rq_bytes(rq)))
337 __blk_mq_end_io(rq, error);
339 EXPORT_SYMBOL(blk_mq_end_io);
341 static void __blk_mq_complete_request_remote(void *data)
343 struct request *rq = data;
345 rq->q->softirq_done_fn(rq);
348 static void blk_mq_ipi_complete_request(struct request *rq)
350 struct blk_mq_ctx *ctx = rq->mq_ctx;
354 if (!test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags)) {
355 rq->q->softirq_done_fn(rq);
360 if (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags))
361 shared = cpus_share_cache(cpu, ctx->cpu);
363 if (cpu != ctx->cpu && !shared && cpu_online(ctx->cpu)) {
364 rq->csd.func = __blk_mq_complete_request_remote;
367 smp_call_function_single_async(ctx->cpu, &rq->csd);
369 rq->q->softirq_done_fn(rq);
374 void __blk_mq_complete_request(struct request *rq)
376 struct request_queue *q = rq->q;
378 if (!q->softirq_done_fn)
379 blk_mq_end_io(rq, rq->errors);
381 blk_mq_ipi_complete_request(rq);
385 * blk_mq_complete_request - end I/O on a request
386 * @rq: the request being processed
389 * Ends all I/O on a request. It does not handle partial completions.
390 * The actual completion happens out-of-order, through a IPI handler.
392 void blk_mq_complete_request(struct request *rq)
394 struct request_queue *q = rq->q;
396 if (unlikely(blk_should_fake_timeout(q)))
398 if (!blk_mark_rq_complete(rq))
399 __blk_mq_complete_request(rq);
401 EXPORT_SYMBOL(blk_mq_complete_request);
403 static void blk_mq_start_request(struct request *rq, bool last)
405 struct request_queue *q = rq->q;
407 trace_block_rq_issue(q, rq);
409 rq->resid_len = blk_rq_bytes(rq);
410 if (unlikely(blk_bidi_rq(rq)))
411 rq->next_rq->resid_len = blk_rq_bytes(rq->next_rq);
414 * Just mark start time and set the started bit. Due to memory
415 * ordering, we know we'll see the correct deadline as long as
416 * REQ_ATOMIC_STARTED is seen. Use the default queue timeout,
417 * unless one has been set in the request.
420 rq->deadline = jiffies + q->rq_timeout;
422 rq->deadline = jiffies + rq->timeout;
425 * Mark us as started and clear complete. Complete might have been
426 * set if requeue raced with timeout, which then marked it as
427 * complete. So be sure to clear complete again when we start
428 * the request, otherwise we'll ignore the completion event.
430 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
431 set_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
432 if (test_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags))
433 clear_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags);
435 if (q->dma_drain_size && blk_rq_bytes(rq)) {
437 * Make sure space for the drain appears. We know we can do
438 * this because max_hw_segments has been adjusted to be one
439 * fewer than the device can handle.
441 rq->nr_phys_segments++;
445 * Flag the last request in the series so that drivers know when IO
446 * should be kicked off, if they don't do it on a per-request basis.
448 * Note: the flag isn't the only condition drivers should do kick off.
449 * If drive is busy, the last request might not have the bit set.
452 rq->cmd_flags |= REQ_END;
455 static void __blk_mq_requeue_request(struct request *rq)
457 struct request_queue *q = rq->q;
459 trace_block_rq_requeue(q, rq);
460 clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
462 rq->cmd_flags &= ~REQ_END;
464 if (q->dma_drain_size && blk_rq_bytes(rq))
465 rq->nr_phys_segments--;
468 void blk_mq_requeue_request(struct request *rq)
470 __blk_mq_requeue_request(rq);
471 blk_clear_rq_complete(rq);
473 BUG_ON(blk_queued_rq(rq));
474 blk_mq_add_to_requeue_list(rq, true);
476 EXPORT_SYMBOL(blk_mq_requeue_request);
478 static void blk_mq_requeue_work(struct work_struct *work)
480 struct request_queue *q =
481 container_of(work, struct request_queue, requeue_work);
483 struct request *rq, *next;
486 spin_lock_irqsave(&q->requeue_lock, flags);
487 list_splice_init(&q->requeue_list, &rq_list);
488 spin_unlock_irqrestore(&q->requeue_lock, flags);
490 list_for_each_entry_safe(rq, next, &rq_list, queuelist) {
491 if (!(rq->cmd_flags & REQ_SOFTBARRIER))
494 rq->cmd_flags &= ~REQ_SOFTBARRIER;
495 list_del_init(&rq->queuelist);
496 blk_mq_insert_request(rq, true, false, false);
499 while (!list_empty(&rq_list)) {
500 rq = list_entry(rq_list.next, struct request, queuelist);
501 list_del_init(&rq->queuelist);
502 blk_mq_insert_request(rq, false, false, false);
505 blk_mq_run_queues(q, false);
508 void blk_mq_add_to_requeue_list(struct request *rq, bool at_head)
510 struct request_queue *q = rq->q;
514 * We abuse this flag that is otherwise used by the I/O scheduler to
515 * request head insertation from the workqueue.
517 BUG_ON(rq->cmd_flags & REQ_SOFTBARRIER);
519 spin_lock_irqsave(&q->requeue_lock, flags);
521 rq->cmd_flags |= REQ_SOFTBARRIER;
522 list_add(&rq->queuelist, &q->requeue_list);
524 list_add_tail(&rq->queuelist, &q->requeue_list);
526 spin_unlock_irqrestore(&q->requeue_lock, flags);
528 EXPORT_SYMBOL(blk_mq_add_to_requeue_list);
530 void blk_mq_kick_requeue_list(struct request_queue *q)
532 kblockd_schedule_work(&q->requeue_work);
534 EXPORT_SYMBOL(blk_mq_kick_requeue_list);
536 static inline bool is_flush_request(struct request *rq, unsigned int tag)
538 return ((rq->cmd_flags & REQ_FLUSH_SEQ) &&
539 rq->q->flush_rq->tag == tag);
542 struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag)
544 struct request *rq = tags->rqs[tag];
546 if (!is_flush_request(rq, tag))
549 return rq->q->flush_rq;
551 EXPORT_SYMBOL(blk_mq_tag_to_rq);
553 struct blk_mq_timeout_data {
554 struct blk_mq_hw_ctx *hctx;
556 unsigned int *next_set;
559 static void blk_mq_timeout_check(void *__data, unsigned long *free_tags)
561 struct blk_mq_timeout_data *data = __data;
562 struct blk_mq_hw_ctx *hctx = data->hctx;
565 /* It may not be in flight yet (this is where
566 * the REQ_ATOMIC_STARTED flag comes in). The requests are
567 * statically allocated, so we know it's always safe to access the
568 * memory associated with a bit offset into ->rqs[].
574 tag = find_next_zero_bit(free_tags, hctx->tags->nr_tags, tag);
575 if (tag >= hctx->tags->nr_tags)
578 rq = blk_mq_tag_to_rq(hctx->tags, tag++);
579 if (rq->q != hctx->queue)
581 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
584 blk_rq_check_expired(rq, data->next, data->next_set);
588 static void blk_mq_hw_ctx_check_timeout(struct blk_mq_hw_ctx *hctx,
590 unsigned int *next_set)
592 struct blk_mq_timeout_data data = {
595 .next_set = next_set,
599 * Ask the tagging code to iterate busy requests, so we can
600 * check them for timeout.
602 blk_mq_tag_busy_iter(hctx->tags, blk_mq_timeout_check, &data);
605 static enum blk_eh_timer_return blk_mq_rq_timed_out(struct request *rq)
607 struct request_queue *q = rq->q;
610 * We know that complete is set at this point. If STARTED isn't set
611 * anymore, then the request isn't active and the "timeout" should
612 * just be ignored. This can happen due to the bitflag ordering.
613 * Timeout first checks if STARTED is set, and if it is, assumes
614 * the request is active. But if we race with completion, then
615 * we both flags will get cleared. So check here again, and ignore
616 * a timeout event with a request that isn't active.
618 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
619 return BLK_EH_NOT_HANDLED;
621 if (!q->mq_ops->timeout)
622 return BLK_EH_RESET_TIMER;
624 return q->mq_ops->timeout(rq);
627 static void blk_mq_rq_timer(unsigned long data)
629 struct request_queue *q = (struct request_queue *) data;
630 struct blk_mq_hw_ctx *hctx;
631 unsigned long next = 0;
634 queue_for_each_hw_ctx(q, hctx, i) {
636 * If not software queues are currently mapped to this
637 * hardware queue, there's nothing to check
639 if (!hctx->nr_ctx || !hctx->tags)
642 blk_mq_hw_ctx_check_timeout(hctx, &next, &next_set);
646 next = blk_rq_timeout(round_jiffies_up(next));
647 mod_timer(&q->timeout, next);
649 queue_for_each_hw_ctx(q, hctx, i)
650 blk_mq_tag_idle(hctx);
655 * Reverse check our software queue for entries that we could potentially
656 * merge with. Currently includes a hand-wavy stop count of 8, to not spend
657 * too much time checking for merges.
659 static bool blk_mq_attempt_merge(struct request_queue *q,
660 struct blk_mq_ctx *ctx, struct bio *bio)
665 list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) {
671 if (!blk_rq_merge_ok(rq, bio))
674 el_ret = blk_try_merge(rq, bio);
675 if (el_ret == ELEVATOR_BACK_MERGE) {
676 if (bio_attempt_back_merge(q, rq, bio)) {
681 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
682 if (bio_attempt_front_merge(q, rq, bio)) {
694 * Process software queues that have been marked busy, splicing them
695 * to the for-dispatch
697 static void flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list)
699 struct blk_mq_ctx *ctx;
702 for (i = 0; i < hctx->ctx_map.map_size; i++) {
703 struct blk_align_bitmap *bm = &hctx->ctx_map.map[i];
704 unsigned int off, bit;
710 off = i * hctx->ctx_map.bits_per_word;
712 bit = find_next_bit(&bm->word, bm->depth, bit);
713 if (bit >= bm->depth)
716 ctx = hctx->ctxs[bit + off];
717 clear_bit(bit, &bm->word);
718 spin_lock(&ctx->lock);
719 list_splice_tail_init(&ctx->rq_list, list);
720 spin_unlock(&ctx->lock);
728 * Run this hardware queue, pulling any software queues mapped to it in.
729 * Note that this function currently has various problems around ordering
730 * of IO. In particular, we'd like FIFO behaviour on handling existing
731 * items on the hctx->dispatch list. Ignore that for now.
733 static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
735 struct request_queue *q = hctx->queue;
740 WARN_ON(!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask));
742 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
748 * Touch any software queue that has pending entries.
750 flush_busy_ctxs(hctx, &rq_list);
753 * If we have previous entries on our dispatch list, grab them
754 * and stuff them at the front for more fair dispatch.
756 if (!list_empty_careful(&hctx->dispatch)) {
757 spin_lock(&hctx->lock);
758 if (!list_empty(&hctx->dispatch))
759 list_splice_init(&hctx->dispatch, &rq_list);
760 spin_unlock(&hctx->lock);
764 * Now process all the entries, sending them to the driver.
767 while (!list_empty(&rq_list)) {
770 rq = list_first_entry(&rq_list, struct request, queuelist);
771 list_del_init(&rq->queuelist);
773 blk_mq_start_request(rq, list_empty(&rq_list));
775 ret = q->mq_ops->queue_rq(hctx, rq);
777 case BLK_MQ_RQ_QUEUE_OK:
780 case BLK_MQ_RQ_QUEUE_BUSY:
781 list_add(&rq->queuelist, &rq_list);
782 __blk_mq_requeue_request(rq);
785 pr_err("blk-mq: bad return on queue: %d\n", ret);
786 case BLK_MQ_RQ_QUEUE_ERROR:
788 blk_mq_end_io(rq, rq->errors);
792 if (ret == BLK_MQ_RQ_QUEUE_BUSY)
797 hctx->dispatched[0]++;
798 else if (queued < (1 << (BLK_MQ_MAX_DISPATCH_ORDER - 1)))
799 hctx->dispatched[ilog2(queued) + 1]++;
802 * Any items that need requeuing? Stuff them into hctx->dispatch,
803 * that is where we will continue on next queue run.
805 if (!list_empty(&rq_list)) {
806 spin_lock(&hctx->lock);
807 list_splice(&rq_list, &hctx->dispatch);
808 spin_unlock(&hctx->lock);
813 * It'd be great if the workqueue API had a way to pass
814 * in a mask and had some smarts for more clever placement.
815 * For now we just round-robin here, switching for every
816 * BLK_MQ_CPU_WORK_BATCH queued items.
818 static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx)
820 int cpu = hctx->next_cpu;
822 if (--hctx->next_cpu_batch <= 0) {
825 next_cpu = cpumask_next(hctx->next_cpu, hctx->cpumask);
826 if (next_cpu >= nr_cpu_ids)
827 next_cpu = cpumask_first(hctx->cpumask);
829 hctx->next_cpu = next_cpu;
830 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
836 void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
838 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
841 if (!async && cpumask_test_cpu(smp_processor_id(), hctx->cpumask))
842 __blk_mq_run_hw_queue(hctx);
843 else if (hctx->queue->nr_hw_queues == 1)
844 kblockd_schedule_delayed_work(&hctx->run_work, 0);
848 cpu = blk_mq_hctx_next_cpu(hctx);
849 kblockd_schedule_delayed_work_on(cpu, &hctx->run_work, 0);
853 void blk_mq_run_queues(struct request_queue *q, bool async)
855 struct blk_mq_hw_ctx *hctx;
858 queue_for_each_hw_ctx(q, hctx, i) {
859 if ((!blk_mq_hctx_has_pending(hctx) &&
860 list_empty_careful(&hctx->dispatch)) ||
861 test_bit(BLK_MQ_S_STOPPED, &hctx->state))
865 blk_mq_run_hw_queue(hctx, async);
869 EXPORT_SYMBOL(blk_mq_run_queues);
871 void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
873 cancel_delayed_work(&hctx->run_work);
874 cancel_delayed_work(&hctx->delay_work);
875 set_bit(BLK_MQ_S_STOPPED, &hctx->state);
877 EXPORT_SYMBOL(blk_mq_stop_hw_queue);
879 void blk_mq_stop_hw_queues(struct request_queue *q)
881 struct blk_mq_hw_ctx *hctx;
884 queue_for_each_hw_ctx(q, hctx, i)
885 blk_mq_stop_hw_queue(hctx);
887 EXPORT_SYMBOL(blk_mq_stop_hw_queues);
889 void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
891 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
894 __blk_mq_run_hw_queue(hctx);
897 EXPORT_SYMBOL(blk_mq_start_hw_queue);
899 void blk_mq_start_hw_queues(struct request_queue *q)
901 struct blk_mq_hw_ctx *hctx;
904 queue_for_each_hw_ctx(q, hctx, i)
905 blk_mq_start_hw_queue(hctx);
907 EXPORT_SYMBOL(blk_mq_start_hw_queues);
910 void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async)
912 struct blk_mq_hw_ctx *hctx;
915 queue_for_each_hw_ctx(q, hctx, i) {
916 if (!test_bit(BLK_MQ_S_STOPPED, &hctx->state))
919 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
921 blk_mq_run_hw_queue(hctx, async);
925 EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
927 static void blk_mq_run_work_fn(struct work_struct *work)
929 struct blk_mq_hw_ctx *hctx;
931 hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work);
933 __blk_mq_run_hw_queue(hctx);
936 static void blk_mq_delay_work_fn(struct work_struct *work)
938 struct blk_mq_hw_ctx *hctx;
940 hctx = container_of(work, struct blk_mq_hw_ctx, delay_work.work);
942 if (test_and_clear_bit(BLK_MQ_S_STOPPED, &hctx->state))
943 __blk_mq_run_hw_queue(hctx);
946 void blk_mq_delay_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
948 unsigned long tmo = msecs_to_jiffies(msecs);
950 if (hctx->queue->nr_hw_queues == 1)
951 kblockd_schedule_delayed_work(&hctx->delay_work, tmo);
955 cpu = blk_mq_hctx_next_cpu(hctx);
956 kblockd_schedule_delayed_work_on(cpu, &hctx->delay_work, tmo);
959 EXPORT_SYMBOL(blk_mq_delay_queue);
961 static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx,
962 struct request *rq, bool at_head)
964 struct blk_mq_ctx *ctx = rq->mq_ctx;
966 trace_block_rq_insert(hctx->queue, rq);
969 list_add(&rq->queuelist, &ctx->rq_list);
971 list_add_tail(&rq->queuelist, &ctx->rq_list);
973 blk_mq_hctx_mark_pending(hctx, ctx);
976 * We do this early, to ensure we are on the right CPU.
981 void blk_mq_insert_request(struct request *rq, bool at_head, bool run_queue,
984 struct request_queue *q = rq->q;
985 struct blk_mq_hw_ctx *hctx;
986 struct blk_mq_ctx *ctx = rq->mq_ctx, *current_ctx;
988 current_ctx = blk_mq_get_ctx(q);
989 if (!cpu_online(ctx->cpu))
990 rq->mq_ctx = ctx = current_ctx;
992 hctx = q->mq_ops->map_queue(q, ctx->cpu);
994 if (rq->cmd_flags & (REQ_FLUSH | REQ_FUA) &&
995 !(rq->cmd_flags & (REQ_FLUSH_SEQ))) {
996 blk_insert_flush(rq);
998 spin_lock(&ctx->lock);
999 __blk_mq_insert_request(hctx, rq, at_head);
1000 spin_unlock(&ctx->lock);
1004 blk_mq_run_hw_queue(hctx, async);
1006 blk_mq_put_ctx(current_ctx);
1009 static void blk_mq_insert_requests(struct request_queue *q,
1010 struct blk_mq_ctx *ctx,
1011 struct list_head *list,
1016 struct blk_mq_hw_ctx *hctx;
1017 struct blk_mq_ctx *current_ctx;
1019 trace_block_unplug(q, depth, !from_schedule);
1021 current_ctx = blk_mq_get_ctx(q);
1023 if (!cpu_online(ctx->cpu))
1025 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1028 * preemption doesn't flush plug list, so it's possible ctx->cpu is
1031 spin_lock(&ctx->lock);
1032 while (!list_empty(list)) {
1035 rq = list_first_entry(list, struct request, queuelist);
1036 list_del_init(&rq->queuelist);
1038 __blk_mq_insert_request(hctx, rq, false);
1040 spin_unlock(&ctx->lock);
1042 blk_mq_run_hw_queue(hctx, from_schedule);
1043 blk_mq_put_ctx(current_ctx);
1046 static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b)
1048 struct request *rqa = container_of(a, struct request, queuelist);
1049 struct request *rqb = container_of(b, struct request, queuelist);
1051 return !(rqa->mq_ctx < rqb->mq_ctx ||
1052 (rqa->mq_ctx == rqb->mq_ctx &&
1053 blk_rq_pos(rqa) < blk_rq_pos(rqb)));
1056 void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
1058 struct blk_mq_ctx *this_ctx;
1059 struct request_queue *this_q;
1062 LIST_HEAD(ctx_list);
1065 list_splice_init(&plug->mq_list, &list);
1067 list_sort(NULL, &list, plug_ctx_cmp);
1073 while (!list_empty(&list)) {
1074 rq = list_entry_rq(list.next);
1075 list_del_init(&rq->queuelist);
1077 if (rq->mq_ctx != this_ctx) {
1079 blk_mq_insert_requests(this_q, this_ctx,
1084 this_ctx = rq->mq_ctx;
1090 list_add_tail(&rq->queuelist, &ctx_list);
1094 * If 'this_ctx' is set, we know we have entries to complete
1095 * on 'ctx_list'. Do those.
1098 blk_mq_insert_requests(this_q, this_ctx, &ctx_list, depth,
1103 static void blk_mq_bio_to_request(struct request *rq, struct bio *bio)
1105 init_request_from_bio(rq, bio);
1107 if (blk_do_io_stat(rq)) {
1108 rq->start_time = jiffies;
1109 blk_account_io_start(rq, 1);
1113 static inline bool blk_mq_merge_queue_io(struct blk_mq_hw_ctx *hctx,
1114 struct blk_mq_ctx *ctx,
1115 struct request *rq, struct bio *bio)
1117 struct request_queue *q = hctx->queue;
1119 if (!(hctx->flags & BLK_MQ_F_SHOULD_MERGE)) {
1120 blk_mq_bio_to_request(rq, bio);
1121 spin_lock(&ctx->lock);
1123 __blk_mq_insert_request(hctx, rq, false);
1124 spin_unlock(&ctx->lock);
1127 spin_lock(&ctx->lock);
1128 if (!blk_mq_attempt_merge(q, ctx, bio)) {
1129 blk_mq_bio_to_request(rq, bio);
1133 spin_unlock(&ctx->lock);
1134 __blk_mq_free_request(hctx, ctx, rq);
1139 struct blk_map_ctx {
1140 struct blk_mq_hw_ctx *hctx;
1141 struct blk_mq_ctx *ctx;
1144 static struct request *blk_mq_map_request(struct request_queue *q,
1146 struct blk_map_ctx *data)
1148 struct blk_mq_hw_ctx *hctx;
1149 struct blk_mq_ctx *ctx;
1151 int rw = bio_data_dir(bio);
1152 struct blk_mq_alloc_data alloc_data;
1154 if (unlikely(blk_mq_queue_enter(q))) {
1155 bio_endio(bio, -EIO);
1159 ctx = blk_mq_get_ctx(q);
1160 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1162 if (rw_is_sync(bio->bi_rw))
1165 trace_block_getrq(q, bio, rw);
1166 blk_mq_set_alloc_data(&alloc_data, q, GFP_ATOMIC, false, ctx,
1168 rq = __blk_mq_alloc_request(&alloc_data, rw);
1169 if (unlikely(!rq)) {
1170 __blk_mq_run_hw_queue(hctx);
1171 blk_mq_put_ctx(ctx);
1172 trace_block_sleeprq(q, bio, rw);
1174 ctx = blk_mq_get_ctx(q);
1175 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1176 blk_mq_set_alloc_data(&alloc_data, q,
1177 __GFP_WAIT|GFP_ATOMIC, false, ctx, hctx);
1178 rq = __blk_mq_alloc_request(&alloc_data, rw);
1179 ctx = alloc_data.ctx;
1180 hctx = alloc_data.hctx;
1190 * Multiple hardware queue variant. This will not use per-process plugs,
1191 * but will attempt to bypass the hctx queueing if we can go straight to
1192 * hardware for SYNC IO.
1194 static void blk_mq_make_request(struct request_queue *q, struct bio *bio)
1196 const int is_sync = rw_is_sync(bio->bi_rw);
1197 const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
1198 struct blk_map_ctx data;
1201 blk_queue_bounce(q, &bio);
1203 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
1204 bio_endio(bio, -EIO);
1208 rq = blk_mq_map_request(q, bio, &data);
1212 if (unlikely(is_flush_fua)) {
1213 blk_mq_bio_to_request(rq, bio);
1214 blk_insert_flush(rq);
1221 blk_mq_bio_to_request(rq, bio);
1222 blk_mq_start_request(rq, true);
1226 * For OK queue, we are done. For error, kill it. Any other
1227 * error (busy), just add it to our list as we previously
1230 ret = q->mq_ops->queue_rq(data.hctx, rq);
1231 if (ret == BLK_MQ_RQ_QUEUE_OK)
1234 __blk_mq_requeue_request(rq);
1236 if (ret == BLK_MQ_RQ_QUEUE_ERROR) {
1238 blk_mq_end_io(rq, rq->errors);
1244 if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) {
1246 * For a SYNC request, send it to the hardware immediately. For
1247 * an ASYNC request, just ensure that we run it later on. The
1248 * latter allows for merging opportunities and more efficient
1252 blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua);
1255 blk_mq_put_ctx(data.ctx);
1259 * Single hardware queue variant. This will attempt to use any per-process
1260 * plug for merging and IO deferral.
1262 static void blk_sq_make_request(struct request_queue *q, struct bio *bio)
1264 const int is_sync = rw_is_sync(bio->bi_rw);
1265 const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
1266 unsigned int use_plug, request_count = 0;
1267 struct blk_map_ctx data;
1271 * If we have multiple hardware queues, just go directly to
1272 * one of those for sync IO.
1274 use_plug = !is_flush_fua && !is_sync;
1276 blk_queue_bounce(q, &bio);
1278 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
1279 bio_endio(bio, -EIO);
1283 if (use_plug && !blk_queue_nomerges(q) &&
1284 blk_attempt_plug_merge(q, bio, &request_count))
1287 rq = blk_mq_map_request(q, bio, &data);
1291 if (unlikely(is_flush_fua)) {
1292 blk_mq_bio_to_request(rq, bio);
1293 blk_insert_flush(rq);
1298 * A task plug currently exists. Since this is completely lockless,
1299 * utilize that to temporarily store requests until the task is
1300 * either done or scheduled away.
1303 struct blk_plug *plug = current->plug;
1306 blk_mq_bio_to_request(rq, bio);
1307 if (list_empty(&plug->mq_list))
1308 trace_block_plug(q);
1309 else if (request_count >= BLK_MAX_REQUEST_COUNT) {
1310 blk_flush_plug_list(plug, false);
1311 trace_block_plug(q);
1313 list_add_tail(&rq->queuelist, &plug->mq_list);
1314 blk_mq_put_ctx(data.ctx);
1319 if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) {
1321 * For a SYNC request, send it to the hardware immediately. For
1322 * an ASYNC request, just ensure that we run it later on. The
1323 * latter allows for merging opportunities and more efficient
1327 blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua);
1330 blk_mq_put_ctx(data.ctx);
1334 * Default mapping to a software queue, since we use one per CPU.
1336 struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q, const int cpu)
1338 return q->queue_hw_ctx[q->mq_map[cpu]];
1340 EXPORT_SYMBOL(blk_mq_map_queue);
1342 static void blk_mq_free_rq_map(struct blk_mq_tag_set *set,
1343 struct blk_mq_tags *tags, unsigned int hctx_idx)
1347 if (tags->rqs && set->ops->exit_request) {
1350 for (i = 0; i < tags->nr_tags; i++) {
1353 set->ops->exit_request(set->driver_data, tags->rqs[i],
1358 while (!list_empty(&tags->page_list)) {
1359 page = list_first_entry(&tags->page_list, struct page, lru);
1360 list_del_init(&page->lru);
1361 __free_pages(page, page->private);
1366 blk_mq_free_tags(tags);
1369 static size_t order_to_size(unsigned int order)
1371 return (size_t)PAGE_SIZE << order;
1374 static struct blk_mq_tags *blk_mq_init_rq_map(struct blk_mq_tag_set *set,
1375 unsigned int hctx_idx)
1377 struct blk_mq_tags *tags;
1378 unsigned int i, j, entries_per_page, max_order = 4;
1379 size_t rq_size, left;
1381 tags = blk_mq_init_tags(set->queue_depth, set->reserved_tags,
1386 INIT_LIST_HEAD(&tags->page_list);
1388 tags->rqs = kmalloc_node(set->queue_depth * sizeof(struct request *),
1389 GFP_KERNEL, set->numa_node);
1391 blk_mq_free_tags(tags);
1396 * rq_size is the size of the request plus driver payload, rounded
1397 * to the cacheline size
1399 rq_size = round_up(sizeof(struct request) + set->cmd_size,
1401 left = rq_size * set->queue_depth;
1403 for (i = 0; i < set->queue_depth; ) {
1404 int this_order = max_order;
1409 while (left < order_to_size(this_order - 1) && this_order)
1413 page = alloc_pages_node(set->numa_node, GFP_KERNEL,
1419 if (order_to_size(this_order) < rq_size)
1426 page->private = this_order;
1427 list_add_tail(&page->lru, &tags->page_list);
1429 p = page_address(page);
1430 entries_per_page = order_to_size(this_order) / rq_size;
1431 to_do = min(entries_per_page, set->queue_depth - i);
1432 left -= to_do * rq_size;
1433 for (j = 0; j < to_do; j++) {
1435 if (set->ops->init_request) {
1436 if (set->ops->init_request(set->driver_data,
1437 tags->rqs[i], hctx_idx, i,
1450 pr_warn("%s: failed to allocate requests\n", __func__);
1451 blk_mq_free_rq_map(set, tags, hctx_idx);
1455 static void blk_mq_free_bitmap(struct blk_mq_ctxmap *bitmap)
1460 static int blk_mq_alloc_bitmap(struct blk_mq_ctxmap *bitmap, int node)
1462 unsigned int bpw = 8, total, num_maps, i;
1464 bitmap->bits_per_word = bpw;
1466 num_maps = ALIGN(nr_cpu_ids, bpw) / bpw;
1467 bitmap->map = kzalloc_node(num_maps * sizeof(struct blk_align_bitmap),
1472 bitmap->map_size = num_maps;
1475 for (i = 0; i < num_maps; i++) {
1476 bitmap->map[i].depth = min(total, bitmap->bits_per_word);
1477 total -= bitmap->map[i].depth;
1483 static int blk_mq_hctx_cpu_offline(struct blk_mq_hw_ctx *hctx, int cpu)
1485 struct request_queue *q = hctx->queue;
1486 struct blk_mq_ctx *ctx;
1490 * Move ctx entries to new CPU, if this one is going away.
1492 ctx = __blk_mq_get_ctx(q, cpu);
1494 spin_lock(&ctx->lock);
1495 if (!list_empty(&ctx->rq_list)) {
1496 list_splice_init(&ctx->rq_list, &tmp);
1497 blk_mq_hctx_clear_pending(hctx, ctx);
1499 spin_unlock(&ctx->lock);
1501 if (list_empty(&tmp))
1504 ctx = blk_mq_get_ctx(q);
1505 spin_lock(&ctx->lock);
1507 while (!list_empty(&tmp)) {
1510 rq = list_first_entry(&tmp, struct request, queuelist);
1512 list_move_tail(&rq->queuelist, &ctx->rq_list);
1515 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1516 blk_mq_hctx_mark_pending(hctx, ctx);
1518 spin_unlock(&ctx->lock);
1520 blk_mq_run_hw_queue(hctx, true);
1521 blk_mq_put_ctx(ctx);
1525 static int blk_mq_hctx_cpu_online(struct blk_mq_hw_ctx *hctx, int cpu)
1527 struct request_queue *q = hctx->queue;
1528 struct blk_mq_tag_set *set = q->tag_set;
1530 if (set->tags[hctx->queue_num])
1533 set->tags[hctx->queue_num] = blk_mq_init_rq_map(set, hctx->queue_num);
1534 if (!set->tags[hctx->queue_num])
1537 hctx->tags = set->tags[hctx->queue_num];
1541 static int blk_mq_hctx_notify(void *data, unsigned long action,
1544 struct blk_mq_hw_ctx *hctx = data;
1546 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN)
1547 return blk_mq_hctx_cpu_offline(hctx, cpu);
1548 else if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN)
1549 return blk_mq_hctx_cpu_online(hctx, cpu);
1554 static void blk_mq_exit_hw_queues(struct request_queue *q,
1555 struct blk_mq_tag_set *set, int nr_queue)
1557 struct blk_mq_hw_ctx *hctx;
1560 queue_for_each_hw_ctx(q, hctx, i) {
1564 blk_mq_tag_idle(hctx);
1566 if (set->ops->exit_hctx)
1567 set->ops->exit_hctx(hctx, i);
1569 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
1571 blk_mq_free_bitmap(&hctx->ctx_map);
1576 static void blk_mq_free_hw_queues(struct request_queue *q,
1577 struct blk_mq_tag_set *set)
1579 struct blk_mq_hw_ctx *hctx;
1582 queue_for_each_hw_ctx(q, hctx, i) {
1583 free_cpumask_var(hctx->cpumask);
1588 static int blk_mq_init_hw_queues(struct request_queue *q,
1589 struct blk_mq_tag_set *set)
1591 struct blk_mq_hw_ctx *hctx;
1595 * Initialize hardware queues
1597 queue_for_each_hw_ctx(q, hctx, i) {
1600 node = hctx->numa_node;
1601 if (node == NUMA_NO_NODE)
1602 node = hctx->numa_node = set->numa_node;
1604 INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn);
1605 INIT_DELAYED_WORK(&hctx->delay_work, blk_mq_delay_work_fn);
1606 spin_lock_init(&hctx->lock);
1607 INIT_LIST_HEAD(&hctx->dispatch);
1609 hctx->queue_num = i;
1610 hctx->flags = set->flags;
1611 hctx->cmd_size = set->cmd_size;
1613 blk_mq_init_cpu_notifier(&hctx->cpu_notifier,
1614 blk_mq_hctx_notify, hctx);
1615 blk_mq_register_cpu_notifier(&hctx->cpu_notifier);
1617 hctx->tags = set->tags[i];
1620 * Allocate space for all possible cpus to avoid allocation in
1623 hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *),
1628 if (blk_mq_alloc_bitmap(&hctx->ctx_map, node))
1633 if (set->ops->init_hctx &&
1634 set->ops->init_hctx(hctx, set->driver_data, i))
1638 if (i == q->nr_hw_queues)
1644 blk_mq_exit_hw_queues(q, set, i);
1649 static void blk_mq_init_cpu_queues(struct request_queue *q,
1650 unsigned int nr_hw_queues)
1654 for_each_possible_cpu(i) {
1655 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
1656 struct blk_mq_hw_ctx *hctx;
1658 memset(__ctx, 0, sizeof(*__ctx));
1660 spin_lock_init(&__ctx->lock);
1661 INIT_LIST_HEAD(&__ctx->rq_list);
1664 /* If the cpu isn't online, the cpu is mapped to first hctx */
1668 hctx = q->mq_ops->map_queue(q, i);
1669 cpumask_set_cpu(i, hctx->cpumask);
1673 * Set local node, IFF we have more than one hw queue. If
1674 * not, we remain on the home node of the device
1676 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
1677 hctx->numa_node = cpu_to_node(i);
1681 static void blk_mq_map_swqueue(struct request_queue *q)
1684 struct blk_mq_hw_ctx *hctx;
1685 struct blk_mq_ctx *ctx;
1687 queue_for_each_hw_ctx(q, hctx, i) {
1688 cpumask_clear(hctx->cpumask);
1693 * Map software to hardware queues
1695 queue_for_each_ctx(q, ctx, i) {
1696 /* If the cpu isn't online, the cpu is mapped to first hctx */
1700 hctx = q->mq_ops->map_queue(q, i);
1701 cpumask_set_cpu(i, hctx->cpumask);
1702 ctx->index_hw = hctx->nr_ctx;
1703 hctx->ctxs[hctx->nr_ctx++] = ctx;
1706 queue_for_each_hw_ctx(q, hctx, i) {
1708 * If not software queues are mapped to this hardware queue,
1709 * disable it and free the request entries
1711 if (!hctx->nr_ctx) {
1712 struct blk_mq_tag_set *set = q->tag_set;
1715 blk_mq_free_rq_map(set, set->tags[i], i);
1716 set->tags[i] = NULL;
1723 * Initialize batch roundrobin counts
1725 hctx->next_cpu = cpumask_first(hctx->cpumask);
1726 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
1730 static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set *set)
1732 struct blk_mq_hw_ctx *hctx;
1733 struct request_queue *q;
1737 if (set->tag_list.next == set->tag_list.prev)
1742 list_for_each_entry(q, &set->tag_list, tag_set_list) {
1743 blk_mq_freeze_queue(q);
1745 queue_for_each_hw_ctx(q, hctx, i) {
1747 hctx->flags |= BLK_MQ_F_TAG_SHARED;
1749 hctx->flags &= ~BLK_MQ_F_TAG_SHARED;
1751 blk_mq_unfreeze_queue(q);
1755 static void blk_mq_del_queue_tag_set(struct request_queue *q)
1757 struct blk_mq_tag_set *set = q->tag_set;
1759 blk_mq_freeze_queue(q);
1761 mutex_lock(&set->tag_list_lock);
1762 list_del_init(&q->tag_set_list);
1763 blk_mq_update_tag_set_depth(set);
1764 mutex_unlock(&set->tag_list_lock);
1766 blk_mq_unfreeze_queue(q);
1769 static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set,
1770 struct request_queue *q)
1774 mutex_lock(&set->tag_list_lock);
1775 list_add_tail(&q->tag_set_list, &set->tag_list);
1776 blk_mq_update_tag_set_depth(set);
1777 mutex_unlock(&set->tag_list_lock);
1780 struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
1782 struct blk_mq_hw_ctx **hctxs;
1783 struct blk_mq_ctx __percpu *ctx;
1784 struct request_queue *q;
1788 ctx = alloc_percpu(struct blk_mq_ctx);
1790 return ERR_PTR(-ENOMEM);
1792 hctxs = kmalloc_node(set->nr_hw_queues * sizeof(*hctxs), GFP_KERNEL,
1798 map = blk_mq_make_queue_map(set);
1802 for (i = 0; i < set->nr_hw_queues; i++) {
1803 int node = blk_mq_hw_queue_to_node(map, i);
1805 hctxs[i] = kzalloc_node(sizeof(struct blk_mq_hw_ctx),
1810 if (!zalloc_cpumask_var(&hctxs[i]->cpumask, GFP_KERNEL))
1813 atomic_set(&hctxs[i]->nr_active, 0);
1814 hctxs[i]->numa_node = node;
1815 hctxs[i]->queue_num = i;
1818 q = blk_alloc_queue_node(GFP_KERNEL, set->numa_node);
1822 if (percpu_counter_init(&q->mq_usage_counter, 0))
1825 setup_timer(&q->timeout, blk_mq_rq_timer, (unsigned long) q);
1826 blk_queue_rq_timeout(q, 30000);
1828 q->nr_queues = nr_cpu_ids;
1829 q->nr_hw_queues = set->nr_hw_queues;
1833 q->queue_hw_ctx = hctxs;
1835 q->mq_ops = set->ops;
1836 q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
1838 if (!(set->flags & BLK_MQ_F_SG_MERGE))
1839 q->queue_flags |= 1 << QUEUE_FLAG_NO_SG_MERGE;
1841 q->sg_reserved_size = INT_MAX;
1843 INIT_WORK(&q->requeue_work, blk_mq_requeue_work);
1844 INIT_LIST_HEAD(&q->requeue_list);
1845 spin_lock_init(&q->requeue_lock);
1847 if (q->nr_hw_queues > 1)
1848 blk_queue_make_request(q, blk_mq_make_request);
1850 blk_queue_make_request(q, blk_sq_make_request);
1852 blk_queue_rq_timed_out(q, blk_mq_rq_timed_out);
1854 blk_queue_rq_timeout(q, set->timeout);
1857 * Do this after blk_queue_make_request() overrides it...
1859 q->nr_requests = set->queue_depth;
1861 if (set->ops->complete)
1862 blk_queue_softirq_done(q, set->ops->complete);
1864 blk_mq_init_flush(q);
1865 blk_mq_init_cpu_queues(q, set->nr_hw_queues);
1867 q->flush_rq = kzalloc(round_up(sizeof(struct request) +
1868 set->cmd_size, cache_line_size()),
1873 if (blk_mq_init_hw_queues(q, set))
1876 mutex_lock(&all_q_mutex);
1877 list_add_tail(&q->all_q_node, &all_q_list);
1878 mutex_unlock(&all_q_mutex);
1880 blk_mq_add_queue_tag_set(set, q);
1882 blk_mq_map_swqueue(q);
1889 blk_cleanup_queue(q);
1892 for (i = 0; i < set->nr_hw_queues; i++) {
1895 free_cpumask_var(hctxs[i]->cpumask);
1902 return ERR_PTR(-ENOMEM);
1904 EXPORT_SYMBOL(blk_mq_init_queue);
1906 void blk_mq_free_queue(struct request_queue *q)
1908 struct blk_mq_tag_set *set = q->tag_set;
1910 blk_mq_del_queue_tag_set(q);
1912 blk_mq_exit_hw_queues(q, set, set->nr_hw_queues);
1913 blk_mq_free_hw_queues(q, set);
1915 percpu_counter_destroy(&q->mq_usage_counter);
1917 free_percpu(q->queue_ctx);
1918 kfree(q->queue_hw_ctx);
1921 q->queue_ctx = NULL;
1922 q->queue_hw_ctx = NULL;
1925 mutex_lock(&all_q_mutex);
1926 list_del_init(&q->all_q_node);
1927 mutex_unlock(&all_q_mutex);
1930 /* Basically redo blk_mq_init_queue with queue frozen */
1931 static void blk_mq_queue_reinit(struct request_queue *q)
1933 blk_mq_freeze_queue(q);
1935 blk_mq_sysfs_unregister(q);
1937 blk_mq_update_queue_map(q->mq_map, q->nr_hw_queues);
1940 * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
1941 * we should change hctx numa_node according to new topology (this
1942 * involves free and re-allocate memory, worthy doing?)
1945 blk_mq_map_swqueue(q);
1947 blk_mq_sysfs_register(q);
1949 blk_mq_unfreeze_queue(q);
1952 static int blk_mq_queue_reinit_notify(struct notifier_block *nb,
1953 unsigned long action, void *hcpu)
1955 struct request_queue *q;
1958 * Before new mappings are established, hotadded cpu might already
1959 * start handling requests. This doesn't break anything as we map
1960 * offline CPUs to first hardware queue. We will re-init the queue
1961 * below to get optimal settings.
1963 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN &&
1964 action != CPU_ONLINE && action != CPU_ONLINE_FROZEN)
1967 mutex_lock(&all_q_mutex);
1968 list_for_each_entry(q, &all_q_list, all_q_node)
1969 blk_mq_queue_reinit(q);
1970 mutex_unlock(&all_q_mutex);
1975 * Alloc a tag set to be associated with one or more request queues.
1976 * May fail with EINVAL for various error conditions. May adjust the
1977 * requested depth down, if if it too large. In that case, the set
1978 * value will be stored in set->queue_depth.
1980 int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
1984 if (!set->nr_hw_queues)
1986 if (!set->queue_depth)
1988 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN)
1991 if (!set->nr_hw_queues || !set->ops->queue_rq || !set->ops->map_queue)
1994 if (set->queue_depth > BLK_MQ_MAX_DEPTH) {
1995 pr_info("blk-mq: reduced tag depth to %u\n",
1997 set->queue_depth = BLK_MQ_MAX_DEPTH;
2000 set->tags = kmalloc_node(set->nr_hw_queues *
2001 sizeof(struct blk_mq_tags *),
2002 GFP_KERNEL, set->numa_node);
2006 for (i = 0; i < set->nr_hw_queues; i++) {
2007 set->tags[i] = blk_mq_init_rq_map(set, i);
2012 mutex_init(&set->tag_list_lock);
2013 INIT_LIST_HEAD(&set->tag_list);
2019 blk_mq_free_rq_map(set, set->tags[i], i);
2023 EXPORT_SYMBOL(blk_mq_alloc_tag_set);
2025 void blk_mq_free_tag_set(struct blk_mq_tag_set *set)
2029 for (i = 0; i < set->nr_hw_queues; i++) {
2031 blk_mq_free_rq_map(set, set->tags[i], i);
2036 EXPORT_SYMBOL(blk_mq_free_tag_set);
2038 int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr)
2040 struct blk_mq_tag_set *set = q->tag_set;
2041 struct blk_mq_hw_ctx *hctx;
2044 if (!set || nr > set->queue_depth)
2048 queue_for_each_hw_ctx(q, hctx, i) {
2049 ret = blk_mq_tag_update_depth(hctx->tags, nr);
2055 q->nr_requests = nr;
2060 void blk_mq_disable_hotplug(void)
2062 mutex_lock(&all_q_mutex);
2065 void blk_mq_enable_hotplug(void)
2067 mutex_unlock(&all_q_mutex);
2070 static int __init blk_mq_init(void)
2074 /* Must be called after percpu_counter_hotcpu_callback() */
2075 hotcpu_notifier(blk_mq_queue_reinit_notify, -10);
2079 subsys_initcall(blk_mq_init);