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>
23 #include <linux/crash_dump.h>
25 #include <trace/events/block.h>
27 #include <linux/blk-mq.h>
30 #include "blk-mq-tag.h"
32 static DEFINE_MUTEX(all_q_mutex);
33 static LIST_HEAD(all_q_list);
35 static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx);
38 * Check if any of the ctx's have pending work in this hardware queue
40 static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx)
44 for (i = 0; i < hctx->ctx_map.map_size; i++)
45 if (hctx->ctx_map.map[i].word)
51 static inline struct blk_align_bitmap *get_bm(struct blk_mq_hw_ctx *hctx,
52 struct blk_mq_ctx *ctx)
54 return &hctx->ctx_map.map[ctx->index_hw / hctx->ctx_map.bits_per_word];
57 #define CTX_TO_BIT(hctx, ctx) \
58 ((ctx)->index_hw & ((hctx)->ctx_map.bits_per_word - 1))
61 * Mark this ctx as having pending work in this hardware queue
63 static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx,
64 struct blk_mq_ctx *ctx)
66 struct blk_align_bitmap *bm = get_bm(hctx, ctx);
68 if (!test_bit(CTX_TO_BIT(hctx, ctx), &bm->word))
69 set_bit(CTX_TO_BIT(hctx, ctx), &bm->word);
72 static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx *hctx,
73 struct blk_mq_ctx *ctx)
75 struct blk_align_bitmap *bm = get_bm(hctx, ctx);
77 clear_bit(CTX_TO_BIT(hctx, ctx), &bm->word);
80 static int blk_mq_queue_enter(struct request_queue *q)
85 if (percpu_ref_tryget_live(&q->mq_usage_counter))
88 ret = wait_event_interruptible(q->mq_freeze_wq,
89 !q->mq_freeze_depth || blk_queue_dying(q));
90 if (blk_queue_dying(q))
97 static void blk_mq_queue_exit(struct request_queue *q)
99 percpu_ref_put(&q->mq_usage_counter);
102 static void blk_mq_usage_counter_release(struct percpu_ref *ref)
104 struct request_queue *q =
105 container_of(ref, struct request_queue, mq_usage_counter);
107 wake_up_all(&q->mq_freeze_wq);
110 void blk_mq_freeze_queue_start(struct request_queue *q)
114 spin_lock_irq(q->queue_lock);
115 freeze = !q->mq_freeze_depth++;
116 spin_unlock_irq(q->queue_lock);
119 percpu_ref_kill(&q->mq_usage_counter);
120 blk_mq_run_queues(q, false);
123 EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_start);
125 static void blk_mq_freeze_queue_wait(struct request_queue *q)
127 wait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->mq_usage_counter));
131 * Guarantee no request is in use, so we can change any data structure of
132 * the queue afterward.
134 void blk_mq_freeze_queue(struct request_queue *q)
136 blk_mq_freeze_queue_start(q);
137 blk_mq_freeze_queue_wait(q);
140 void blk_mq_unfreeze_queue(struct request_queue *q)
144 spin_lock_irq(q->queue_lock);
145 wake = !--q->mq_freeze_depth;
146 WARN_ON_ONCE(q->mq_freeze_depth < 0);
147 spin_unlock_irq(q->queue_lock);
149 percpu_ref_reinit(&q->mq_usage_counter);
150 wake_up_all(&q->mq_freeze_wq);
153 EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue);
155 void blk_mq_wake_waiters(struct request_queue *q)
157 struct blk_mq_hw_ctx *hctx;
160 queue_for_each_hw_ctx(q, hctx, i)
161 if (blk_mq_hw_queue_mapped(hctx))
162 blk_mq_tag_wakeup_all(hctx->tags, true);
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 rq->start_time = jiffies;
189 #ifdef CONFIG_BLK_CGROUP
191 set_start_time_ns(rq);
192 rq->io_start_time_ns = 0;
194 rq->nr_phys_segments = 0;
195 #if defined(CONFIG_BLK_DEV_INTEGRITY)
196 rq->nr_integrity_segments = 0;
199 /* tag was already set */
209 INIT_LIST_HEAD(&rq->timeout_list);
213 rq->end_io_data = NULL;
216 ctx->rq_dispatched[rw_is_sync(rw_flags)]++;
219 static struct request *
220 __blk_mq_alloc_request(struct blk_mq_alloc_data *data, int rw)
225 tag = blk_mq_get_tag(data);
226 if (tag != BLK_MQ_TAG_FAIL) {
227 rq = data->hctx->tags->rqs[tag];
229 if (blk_mq_tag_busy(data->hctx)) {
230 rq->cmd_flags = REQ_MQ_INFLIGHT;
231 atomic_inc(&data->hctx->nr_active);
235 blk_mq_rq_ctx_init(data->q, data->ctx, rq, rw);
242 struct request *blk_mq_alloc_request(struct request_queue *q, int rw, gfp_t gfp,
245 struct blk_mq_ctx *ctx;
246 struct blk_mq_hw_ctx *hctx;
248 struct blk_mq_alloc_data alloc_data;
251 ret = blk_mq_queue_enter(q);
255 ctx = blk_mq_get_ctx(q);
256 hctx = q->mq_ops->map_queue(q, ctx->cpu);
257 blk_mq_set_alloc_data(&alloc_data, q, gfp & ~__GFP_WAIT,
258 reserved, ctx, hctx);
260 rq = __blk_mq_alloc_request(&alloc_data, rw);
261 if (!rq && (gfp & __GFP_WAIT)) {
262 __blk_mq_run_hw_queue(hctx);
265 ctx = blk_mq_get_ctx(q);
266 hctx = q->mq_ops->map_queue(q, ctx->cpu);
267 blk_mq_set_alloc_data(&alloc_data, q, gfp, reserved, ctx,
269 rq = __blk_mq_alloc_request(&alloc_data, rw);
270 ctx = alloc_data.ctx;
274 blk_mq_queue_exit(q);
275 return ERR_PTR(-EWOULDBLOCK);
279 EXPORT_SYMBOL(blk_mq_alloc_request);
281 static void __blk_mq_free_request(struct blk_mq_hw_ctx *hctx,
282 struct blk_mq_ctx *ctx, struct request *rq)
284 const int tag = rq->tag;
285 struct request_queue *q = rq->q;
287 if (rq->cmd_flags & REQ_MQ_INFLIGHT)
288 atomic_dec(&hctx->nr_active);
291 clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
292 blk_mq_put_tag(hctx, tag, &ctx->last_tag);
293 blk_mq_queue_exit(q);
296 void blk_mq_free_hctx_request(struct blk_mq_hw_ctx *hctx, struct request *rq)
298 struct blk_mq_ctx *ctx = rq->mq_ctx;
300 ctx->rq_completed[rq_is_sync(rq)]++;
301 __blk_mq_free_request(hctx, ctx, rq);
304 EXPORT_SYMBOL_GPL(blk_mq_free_hctx_request);
306 void blk_mq_free_request(struct request *rq)
308 struct blk_mq_hw_ctx *hctx;
309 struct request_queue *q = rq->q;
311 hctx = q->mq_ops->map_queue(q, rq->mq_ctx->cpu);
312 blk_mq_free_hctx_request(hctx, rq);
314 EXPORT_SYMBOL_GPL(blk_mq_free_request);
316 inline void __blk_mq_end_request(struct request *rq, int error)
318 blk_account_io_done(rq);
321 rq->end_io(rq, error);
323 if (unlikely(blk_bidi_rq(rq)))
324 blk_mq_free_request(rq->next_rq);
325 blk_mq_free_request(rq);
328 EXPORT_SYMBOL(__blk_mq_end_request);
330 void blk_mq_end_request(struct request *rq, int error)
332 if (blk_update_request(rq, error, blk_rq_bytes(rq)))
334 __blk_mq_end_request(rq, error);
336 EXPORT_SYMBOL(blk_mq_end_request);
338 static void __blk_mq_complete_request_remote(void *data)
340 struct request *rq = data;
342 rq->q->softirq_done_fn(rq);
345 static void blk_mq_ipi_complete_request(struct request *rq)
347 struct blk_mq_ctx *ctx = rq->mq_ctx;
351 if (!test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags)) {
352 rq->q->softirq_done_fn(rq);
357 if (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags))
358 shared = cpus_share_cache(cpu, ctx->cpu);
360 if (cpu != ctx->cpu && !shared && cpu_online(ctx->cpu)) {
361 rq->csd.func = __blk_mq_complete_request_remote;
364 smp_call_function_single_async(ctx->cpu, &rq->csd);
366 rq->q->softirq_done_fn(rq);
371 void __blk_mq_complete_request(struct request *rq)
373 struct request_queue *q = rq->q;
375 if (!q->softirq_done_fn)
376 blk_mq_end_request(rq, rq->errors);
378 blk_mq_ipi_complete_request(rq);
382 * blk_mq_complete_request - end I/O on a request
383 * @rq: the request being processed
386 * Ends all I/O on a request. It does not handle partial completions.
387 * The actual completion happens out-of-order, through a IPI handler.
389 void blk_mq_complete_request(struct request *rq)
391 struct request_queue *q = rq->q;
393 if (unlikely(blk_should_fake_timeout(q)))
395 if (!blk_mark_rq_complete(rq))
396 __blk_mq_complete_request(rq);
398 EXPORT_SYMBOL(blk_mq_complete_request);
400 void blk_mq_start_request(struct request *rq)
402 struct request_queue *q = rq->q;
404 trace_block_rq_issue(q, rq);
406 rq->resid_len = blk_rq_bytes(rq);
407 if (unlikely(blk_bidi_rq(rq)))
408 rq->next_rq->resid_len = blk_rq_bytes(rq->next_rq);
413 * Ensure that ->deadline is visible before set the started
414 * flag and clear the completed flag.
416 smp_mb__before_atomic();
419 * Mark us as started and clear complete. Complete might have been
420 * set if requeue raced with timeout, which then marked it as
421 * complete. So be sure to clear complete again when we start
422 * the request, otherwise we'll ignore the completion event.
424 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
425 set_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
426 if (test_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags))
427 clear_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags);
429 if (q->dma_drain_size && blk_rq_bytes(rq)) {
431 * Make sure space for the drain appears. We know we can do
432 * this because max_hw_segments has been adjusted to be one
433 * fewer than the device can handle.
435 rq->nr_phys_segments++;
438 EXPORT_SYMBOL(blk_mq_start_request);
440 static void __blk_mq_requeue_request(struct request *rq)
442 struct request_queue *q = rq->q;
444 trace_block_rq_requeue(q, rq);
446 if (test_and_clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags)) {
447 if (q->dma_drain_size && blk_rq_bytes(rq))
448 rq->nr_phys_segments--;
452 void blk_mq_requeue_request(struct request *rq)
454 __blk_mq_requeue_request(rq);
456 BUG_ON(blk_queued_rq(rq));
457 blk_mq_add_to_requeue_list(rq, true);
459 EXPORT_SYMBOL(blk_mq_requeue_request);
461 static void blk_mq_requeue_work(struct work_struct *work)
463 struct request_queue *q =
464 container_of(work, struct request_queue, requeue_work);
466 struct request *rq, *next;
469 spin_lock_irqsave(&q->requeue_lock, flags);
470 list_splice_init(&q->requeue_list, &rq_list);
471 spin_unlock_irqrestore(&q->requeue_lock, flags);
473 list_for_each_entry_safe(rq, next, &rq_list, queuelist) {
474 if (!(rq->cmd_flags & REQ_SOFTBARRIER))
477 rq->cmd_flags &= ~REQ_SOFTBARRIER;
478 list_del_init(&rq->queuelist);
479 blk_mq_insert_request(rq, true, false, false);
482 while (!list_empty(&rq_list)) {
483 rq = list_entry(rq_list.next, struct request, queuelist);
484 list_del_init(&rq->queuelist);
485 blk_mq_insert_request(rq, false, false, false);
489 * Use the start variant of queue running here, so that running
490 * the requeue work will kick stopped queues.
492 blk_mq_start_hw_queues(q);
495 void blk_mq_add_to_requeue_list(struct request *rq, bool at_head)
497 struct request_queue *q = rq->q;
501 * We abuse this flag that is otherwise used by the I/O scheduler to
502 * request head insertation from the workqueue.
504 BUG_ON(rq->cmd_flags & REQ_SOFTBARRIER);
506 spin_lock_irqsave(&q->requeue_lock, flags);
508 rq->cmd_flags |= REQ_SOFTBARRIER;
509 list_add(&rq->queuelist, &q->requeue_list);
511 list_add_tail(&rq->queuelist, &q->requeue_list);
513 spin_unlock_irqrestore(&q->requeue_lock, flags);
515 EXPORT_SYMBOL(blk_mq_add_to_requeue_list);
517 void blk_mq_kick_requeue_list(struct request_queue *q)
519 kblockd_schedule_work(&q->requeue_work);
521 EXPORT_SYMBOL(blk_mq_kick_requeue_list);
523 static inline bool is_flush_request(struct request *rq,
524 struct blk_flush_queue *fq, unsigned int tag)
526 return ((rq->cmd_flags & REQ_FLUSH_SEQ) &&
527 fq->flush_rq->tag == tag);
530 struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag)
532 struct request *rq = tags->rqs[tag];
533 /* mq_ctx of flush rq is always cloned from the corresponding req */
534 struct blk_flush_queue *fq = blk_get_flush_queue(rq->q, rq->mq_ctx);
536 if (!is_flush_request(rq, fq, tag))
541 EXPORT_SYMBOL(blk_mq_tag_to_rq);
543 struct blk_mq_timeout_data {
545 unsigned int next_set;
548 void blk_mq_rq_timed_out(struct request *req, bool reserved)
550 struct blk_mq_ops *ops = req->q->mq_ops;
551 enum blk_eh_timer_return ret = BLK_EH_RESET_TIMER;
554 * We know that complete is set at this point. If STARTED isn't set
555 * anymore, then the request isn't active and the "timeout" should
556 * just be ignored. This can happen due to the bitflag ordering.
557 * Timeout first checks if STARTED is set, and if it is, assumes
558 * the request is active. But if we race with completion, then
559 * we both flags will get cleared. So check here again, and ignore
560 * a timeout event with a request that isn't active.
562 if (!test_bit(REQ_ATOM_STARTED, &req->atomic_flags))
566 ret = ops->timeout(req, reserved);
570 __blk_mq_complete_request(req);
572 case BLK_EH_RESET_TIMER:
574 blk_clear_rq_complete(req);
576 case BLK_EH_NOT_HANDLED:
579 printk(KERN_ERR "block: bad eh return: %d\n", ret);
584 static void blk_mq_check_expired(struct blk_mq_hw_ctx *hctx,
585 struct request *rq, void *priv, bool reserved)
587 struct blk_mq_timeout_data *data = priv;
589 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
592 if (time_after_eq(jiffies, rq->deadline)) {
593 if (!blk_mark_rq_complete(rq))
594 blk_mq_rq_timed_out(rq, reserved);
595 } else if (!data->next_set || time_after(data->next, rq->deadline)) {
596 data->next = rq->deadline;
601 static void blk_mq_rq_timer(unsigned long priv)
603 struct request_queue *q = (struct request_queue *)priv;
604 struct blk_mq_timeout_data data = {
608 struct blk_mq_hw_ctx *hctx;
611 queue_for_each_hw_ctx(q, hctx, i) {
613 * If not software queues are currently mapped to this
614 * hardware queue, there's nothing to check
616 if (!blk_mq_hw_queue_mapped(hctx))
619 blk_mq_tag_busy_iter(hctx, blk_mq_check_expired, &data);
623 data.next = blk_rq_timeout(round_jiffies_up(data.next));
624 mod_timer(&q->timeout, data.next);
626 queue_for_each_hw_ctx(q, hctx, i)
627 blk_mq_tag_idle(hctx);
632 * Reverse check our software queue for entries that we could potentially
633 * merge with. Currently includes a hand-wavy stop count of 8, to not spend
634 * too much time checking for merges.
636 static bool blk_mq_attempt_merge(struct request_queue *q,
637 struct blk_mq_ctx *ctx, struct bio *bio)
642 list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) {
648 if (!blk_rq_merge_ok(rq, bio))
651 el_ret = blk_try_merge(rq, bio);
652 if (el_ret == ELEVATOR_BACK_MERGE) {
653 if (bio_attempt_back_merge(q, rq, bio)) {
658 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
659 if (bio_attempt_front_merge(q, rq, bio)) {
671 * Process software queues that have been marked busy, splicing them
672 * to the for-dispatch
674 static void flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list)
676 struct blk_mq_ctx *ctx;
679 for (i = 0; i < hctx->ctx_map.map_size; i++) {
680 struct blk_align_bitmap *bm = &hctx->ctx_map.map[i];
681 unsigned int off, bit;
687 off = i * hctx->ctx_map.bits_per_word;
689 bit = find_next_bit(&bm->word, bm->depth, bit);
690 if (bit >= bm->depth)
693 ctx = hctx->ctxs[bit + off];
694 clear_bit(bit, &bm->word);
695 spin_lock(&ctx->lock);
696 list_splice_tail_init(&ctx->rq_list, list);
697 spin_unlock(&ctx->lock);
705 * Run this hardware queue, pulling any software queues mapped to it in.
706 * Note that this function currently has various problems around ordering
707 * of IO. In particular, we'd like FIFO behaviour on handling existing
708 * items on the hctx->dispatch list. Ignore that for now.
710 static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
712 struct request_queue *q = hctx->queue;
715 LIST_HEAD(driver_list);
716 struct list_head *dptr;
719 WARN_ON(!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask));
721 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
727 * Touch any software queue that has pending entries.
729 flush_busy_ctxs(hctx, &rq_list);
732 * If we have previous entries on our dispatch list, grab them
733 * and stuff them at the front for more fair dispatch.
735 if (!list_empty_careful(&hctx->dispatch)) {
736 spin_lock(&hctx->lock);
737 if (!list_empty(&hctx->dispatch))
738 list_splice_init(&hctx->dispatch, &rq_list);
739 spin_unlock(&hctx->lock);
743 * Start off with dptr being NULL, so we start the first request
744 * immediately, even if we have more pending.
749 * Now process all the entries, sending them to the driver.
752 while (!list_empty(&rq_list)) {
753 struct blk_mq_queue_data bd;
756 rq = list_first_entry(&rq_list, struct request, queuelist);
757 list_del_init(&rq->queuelist);
761 bd.last = list_empty(&rq_list);
763 ret = q->mq_ops->queue_rq(hctx, &bd);
765 case BLK_MQ_RQ_QUEUE_OK:
768 case BLK_MQ_RQ_QUEUE_BUSY:
769 list_add(&rq->queuelist, &rq_list);
770 __blk_mq_requeue_request(rq);
773 pr_err("blk-mq: bad return on queue: %d\n", ret);
774 case BLK_MQ_RQ_QUEUE_ERROR:
776 blk_mq_end_request(rq, rq->errors);
780 if (ret == BLK_MQ_RQ_QUEUE_BUSY)
784 * We've done the first request. If we have more than 1
785 * left in the list, set dptr to defer issue.
787 if (!dptr && rq_list.next != rq_list.prev)
792 hctx->dispatched[0]++;
793 else if (queued < (1 << (BLK_MQ_MAX_DISPATCH_ORDER - 1)))
794 hctx->dispatched[ilog2(queued) + 1]++;
797 * Any items that need requeuing? Stuff them into hctx->dispatch,
798 * that is where we will continue on next queue run.
800 if (!list_empty(&rq_list)) {
801 spin_lock(&hctx->lock);
802 list_splice(&rq_list, &hctx->dispatch);
803 spin_unlock(&hctx->lock);
808 * It'd be great if the workqueue API had a way to pass
809 * in a mask and had some smarts for more clever placement.
810 * For now we just round-robin here, switching for every
811 * BLK_MQ_CPU_WORK_BATCH queued items.
813 static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx)
815 if (hctx->queue->nr_hw_queues == 1)
816 return WORK_CPU_UNBOUND;
818 if (--hctx->next_cpu_batch <= 0) {
819 int cpu = hctx->next_cpu, next_cpu;
821 next_cpu = cpumask_next(hctx->next_cpu, hctx->cpumask);
822 if (next_cpu >= nr_cpu_ids)
823 next_cpu = cpumask_first(hctx->cpumask);
825 hctx->next_cpu = next_cpu;
826 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
831 return hctx->next_cpu;
834 void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
836 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state) ||
837 !blk_mq_hw_queue_mapped(hctx)))
842 if (cpumask_test_cpu(cpu, hctx->cpumask)) {
843 __blk_mq_run_hw_queue(hctx);
851 kblockd_schedule_delayed_work_on(blk_mq_hctx_next_cpu(hctx),
855 void blk_mq_run_queues(struct request_queue *q, bool async)
857 struct blk_mq_hw_ctx *hctx;
860 queue_for_each_hw_ctx(q, hctx, i) {
861 if ((!blk_mq_hctx_has_pending(hctx) &&
862 list_empty_careful(&hctx->dispatch)) ||
863 test_bit(BLK_MQ_S_STOPPED, &hctx->state))
866 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);
893 blk_mq_run_hw_queue(hctx, false);
895 EXPORT_SYMBOL(blk_mq_start_hw_queue);
897 void blk_mq_start_hw_queues(struct request_queue *q)
899 struct blk_mq_hw_ctx *hctx;
902 queue_for_each_hw_ctx(q, hctx, i)
903 blk_mq_start_hw_queue(hctx);
905 EXPORT_SYMBOL(blk_mq_start_hw_queues);
908 void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async)
910 struct blk_mq_hw_ctx *hctx;
913 queue_for_each_hw_ctx(q, hctx, i) {
914 if (!test_bit(BLK_MQ_S_STOPPED, &hctx->state))
917 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
918 blk_mq_run_hw_queue(hctx, async);
921 EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
923 static void blk_mq_run_work_fn(struct work_struct *work)
925 struct blk_mq_hw_ctx *hctx;
927 hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work);
929 __blk_mq_run_hw_queue(hctx);
932 static void blk_mq_delay_work_fn(struct work_struct *work)
934 struct blk_mq_hw_ctx *hctx;
936 hctx = container_of(work, struct blk_mq_hw_ctx, delay_work.work);
938 if (test_and_clear_bit(BLK_MQ_S_STOPPED, &hctx->state))
939 __blk_mq_run_hw_queue(hctx);
942 void blk_mq_delay_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
944 if (unlikely(!blk_mq_hw_queue_mapped(hctx)))
947 kblockd_schedule_delayed_work_on(blk_mq_hctx_next_cpu(hctx),
948 &hctx->delay_work, msecs_to_jiffies(msecs));
950 EXPORT_SYMBOL(blk_mq_delay_queue);
952 static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx,
953 struct request *rq, bool at_head)
955 struct blk_mq_ctx *ctx = rq->mq_ctx;
957 trace_block_rq_insert(hctx->queue, rq);
960 list_add(&rq->queuelist, &ctx->rq_list);
962 list_add_tail(&rq->queuelist, &ctx->rq_list);
964 blk_mq_hctx_mark_pending(hctx, ctx);
967 void blk_mq_insert_request(struct request *rq, bool at_head, bool run_queue,
970 struct request_queue *q = rq->q;
971 struct blk_mq_hw_ctx *hctx;
972 struct blk_mq_ctx *ctx = rq->mq_ctx, *current_ctx;
974 current_ctx = blk_mq_get_ctx(q);
975 if (!cpu_online(ctx->cpu))
976 rq->mq_ctx = ctx = current_ctx;
978 hctx = q->mq_ops->map_queue(q, ctx->cpu);
980 spin_lock(&ctx->lock);
981 __blk_mq_insert_request(hctx, rq, at_head);
982 spin_unlock(&ctx->lock);
985 blk_mq_run_hw_queue(hctx, async);
987 blk_mq_put_ctx(current_ctx);
990 static void blk_mq_insert_requests(struct request_queue *q,
991 struct blk_mq_ctx *ctx,
992 struct list_head *list,
997 struct blk_mq_hw_ctx *hctx;
998 struct blk_mq_ctx *current_ctx;
1000 trace_block_unplug(q, depth, !from_schedule);
1002 current_ctx = blk_mq_get_ctx(q);
1004 if (!cpu_online(ctx->cpu))
1006 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1009 * preemption doesn't flush plug list, so it's possible ctx->cpu is
1012 spin_lock(&ctx->lock);
1013 while (!list_empty(list)) {
1016 rq = list_first_entry(list, struct request, queuelist);
1017 list_del_init(&rq->queuelist);
1019 __blk_mq_insert_request(hctx, rq, false);
1021 spin_unlock(&ctx->lock);
1023 blk_mq_run_hw_queue(hctx, from_schedule);
1024 blk_mq_put_ctx(current_ctx);
1027 static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b)
1029 struct request *rqa = container_of(a, struct request, queuelist);
1030 struct request *rqb = container_of(b, struct request, queuelist);
1032 return !(rqa->mq_ctx < rqb->mq_ctx ||
1033 (rqa->mq_ctx == rqb->mq_ctx &&
1034 blk_rq_pos(rqa) < blk_rq_pos(rqb)));
1037 void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
1039 struct blk_mq_ctx *this_ctx;
1040 struct request_queue *this_q;
1043 LIST_HEAD(ctx_list);
1046 list_splice_init(&plug->mq_list, &list);
1048 list_sort(NULL, &list, plug_ctx_cmp);
1054 while (!list_empty(&list)) {
1055 rq = list_entry_rq(list.next);
1056 list_del_init(&rq->queuelist);
1058 if (rq->mq_ctx != this_ctx) {
1060 blk_mq_insert_requests(this_q, this_ctx,
1065 this_ctx = rq->mq_ctx;
1071 list_add_tail(&rq->queuelist, &ctx_list);
1075 * If 'this_ctx' is set, we know we have entries to complete
1076 * on 'ctx_list'. Do those.
1079 blk_mq_insert_requests(this_q, this_ctx, &ctx_list, depth,
1084 static void blk_mq_bio_to_request(struct request *rq, struct bio *bio)
1086 init_request_from_bio(rq, bio);
1088 if (blk_do_io_stat(rq))
1089 blk_account_io_start(rq, 1);
1092 static inline bool hctx_allow_merges(struct blk_mq_hw_ctx *hctx)
1094 return (hctx->flags & BLK_MQ_F_SHOULD_MERGE) &&
1095 !blk_queue_nomerges(hctx->queue);
1098 static inline bool blk_mq_merge_queue_io(struct blk_mq_hw_ctx *hctx,
1099 struct blk_mq_ctx *ctx,
1100 struct request *rq, struct bio *bio)
1102 if (!hctx_allow_merges(hctx)) {
1103 blk_mq_bio_to_request(rq, bio);
1104 spin_lock(&ctx->lock);
1106 __blk_mq_insert_request(hctx, rq, false);
1107 spin_unlock(&ctx->lock);
1110 struct request_queue *q = hctx->queue;
1112 spin_lock(&ctx->lock);
1113 if (!blk_mq_attempt_merge(q, ctx, bio)) {
1114 blk_mq_bio_to_request(rq, bio);
1118 spin_unlock(&ctx->lock);
1119 __blk_mq_free_request(hctx, ctx, rq);
1124 struct blk_map_ctx {
1125 struct blk_mq_hw_ctx *hctx;
1126 struct blk_mq_ctx *ctx;
1129 static struct request *blk_mq_map_request(struct request_queue *q,
1131 struct blk_map_ctx *data)
1133 struct blk_mq_hw_ctx *hctx;
1134 struct blk_mq_ctx *ctx;
1136 int rw = bio_data_dir(bio);
1137 struct blk_mq_alloc_data alloc_data;
1139 if (unlikely(blk_mq_queue_enter(q))) {
1140 bio_endio(bio, -EIO);
1144 ctx = blk_mq_get_ctx(q);
1145 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1147 if (rw_is_sync(bio->bi_rw))
1150 trace_block_getrq(q, bio, rw);
1151 blk_mq_set_alloc_data(&alloc_data, q, GFP_ATOMIC, false, ctx,
1153 rq = __blk_mq_alloc_request(&alloc_data, rw);
1154 if (unlikely(!rq)) {
1155 __blk_mq_run_hw_queue(hctx);
1156 blk_mq_put_ctx(ctx);
1157 trace_block_sleeprq(q, bio, rw);
1159 ctx = blk_mq_get_ctx(q);
1160 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1161 blk_mq_set_alloc_data(&alloc_data, q,
1162 __GFP_WAIT|GFP_ATOMIC, false, ctx, hctx);
1163 rq = __blk_mq_alloc_request(&alloc_data, rw);
1164 ctx = alloc_data.ctx;
1165 hctx = alloc_data.hctx;
1175 * Multiple hardware queue variant. This will not use per-process plugs,
1176 * but will attempt to bypass the hctx queueing if we can go straight to
1177 * hardware for SYNC IO.
1179 static void blk_mq_make_request(struct request_queue *q, struct bio *bio)
1181 const int is_sync = rw_is_sync(bio->bi_rw);
1182 const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
1183 struct blk_map_ctx data;
1186 blk_queue_bounce(q, &bio);
1188 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
1189 bio_endio(bio, -EIO);
1193 rq = blk_mq_map_request(q, bio, &data);
1197 if (unlikely(is_flush_fua)) {
1198 blk_mq_bio_to_request(rq, bio);
1199 blk_insert_flush(rq);
1204 * If the driver supports defer issued based on 'last', then
1205 * queue it up like normal since we can potentially save some
1208 if (is_sync && !(data.hctx->flags & BLK_MQ_F_DEFER_ISSUE)) {
1209 struct blk_mq_queue_data bd = {
1216 blk_mq_bio_to_request(rq, bio);
1219 * For OK queue, we are done. For error, kill it. Any other
1220 * error (busy), just add it to our list as we previously
1223 ret = q->mq_ops->queue_rq(data.hctx, &bd);
1224 if (ret == BLK_MQ_RQ_QUEUE_OK)
1227 __blk_mq_requeue_request(rq);
1229 if (ret == BLK_MQ_RQ_QUEUE_ERROR) {
1231 blk_mq_end_request(rq, rq->errors);
1237 if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) {
1239 * For a SYNC request, send it to the hardware immediately. For
1240 * an ASYNC request, just ensure that we run it later on. The
1241 * latter allows for merging opportunities and more efficient
1245 blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua);
1248 blk_mq_put_ctx(data.ctx);
1252 * Single hardware queue variant. This will attempt to use any per-process
1253 * plug for merging and IO deferral.
1255 static void blk_sq_make_request(struct request_queue *q, struct bio *bio)
1257 const int is_sync = rw_is_sync(bio->bi_rw);
1258 const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
1259 unsigned int use_plug, request_count = 0;
1260 struct blk_map_ctx data;
1264 * If we have multiple hardware queues, just go directly to
1265 * one of those for sync IO.
1267 use_plug = !is_flush_fua && !is_sync;
1269 blk_queue_bounce(q, &bio);
1271 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
1272 bio_endio(bio, -EIO);
1276 if (use_plug && !blk_queue_nomerges(q) &&
1277 blk_attempt_plug_merge(q, bio, &request_count))
1280 rq = blk_mq_map_request(q, bio, &data);
1284 if (unlikely(is_flush_fua)) {
1285 blk_mq_bio_to_request(rq, bio);
1286 blk_insert_flush(rq);
1291 * A task plug currently exists. Since this is completely lockless,
1292 * utilize that to temporarily store requests until the task is
1293 * either done or scheduled away.
1296 struct blk_plug *plug = current->plug;
1299 blk_mq_bio_to_request(rq, bio);
1300 if (list_empty(&plug->mq_list))
1301 trace_block_plug(q);
1302 else if (request_count >= BLK_MAX_REQUEST_COUNT) {
1303 blk_flush_plug_list(plug, false);
1304 trace_block_plug(q);
1306 list_add_tail(&rq->queuelist, &plug->mq_list);
1307 blk_mq_put_ctx(data.ctx);
1312 if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) {
1314 * For a SYNC request, send it to the hardware immediately. For
1315 * an ASYNC request, just ensure that we run it later on. The
1316 * latter allows for merging opportunities and more efficient
1320 blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua);
1323 blk_mq_put_ctx(data.ctx);
1327 * Default mapping to a software queue, since we use one per CPU.
1329 struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q, const int cpu)
1331 return q->queue_hw_ctx[q->mq_map[cpu]];
1333 EXPORT_SYMBOL(blk_mq_map_queue);
1335 static void blk_mq_free_rq_map(struct blk_mq_tag_set *set,
1336 struct blk_mq_tags *tags, unsigned int hctx_idx)
1340 if (tags->rqs && set->ops->exit_request) {
1343 for (i = 0; i < tags->nr_tags; i++) {
1346 set->ops->exit_request(set->driver_data, tags->rqs[i],
1348 tags->rqs[i] = NULL;
1352 while (!list_empty(&tags->page_list)) {
1353 page = list_first_entry(&tags->page_list, struct page, lru);
1354 list_del_init(&page->lru);
1355 __free_pages(page, page->private);
1360 blk_mq_free_tags(tags);
1363 static size_t order_to_size(unsigned int order)
1365 return (size_t)PAGE_SIZE << order;
1368 static struct blk_mq_tags *blk_mq_init_rq_map(struct blk_mq_tag_set *set,
1369 unsigned int hctx_idx)
1371 struct blk_mq_tags *tags;
1372 unsigned int i, j, entries_per_page, max_order = 4;
1373 size_t rq_size, left;
1375 tags = blk_mq_init_tags(set->queue_depth, set->reserved_tags,
1380 INIT_LIST_HEAD(&tags->page_list);
1382 tags->rqs = kzalloc_node(set->queue_depth * sizeof(struct request *),
1383 GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY,
1386 blk_mq_free_tags(tags);
1391 * rq_size is the size of the request plus driver payload, rounded
1392 * to the cacheline size
1394 rq_size = round_up(sizeof(struct request) + set->cmd_size,
1396 left = rq_size * set->queue_depth;
1398 for (i = 0; i < set->queue_depth; ) {
1399 int this_order = max_order;
1404 while (left < order_to_size(this_order - 1) && this_order)
1408 page = alloc_pages_node(set->numa_node,
1409 GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY,
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 tags->rqs[i]->atomic_flags = 0;
1432 tags->rqs[i]->cmd_flags = 0;
1433 if (set->ops->init_request) {
1434 if (set->ops->init_request(set->driver_data,
1435 tags->rqs[i], hctx_idx, i,
1437 tags->rqs[i] = NULL;
1450 blk_mq_free_rq_map(set, tags, hctx_idx);
1454 static void blk_mq_free_bitmap(struct blk_mq_ctxmap *bitmap)
1459 static int blk_mq_alloc_bitmap(struct blk_mq_ctxmap *bitmap, int node)
1461 unsigned int bpw = 8, total, num_maps, i;
1463 bitmap->bits_per_word = bpw;
1465 num_maps = ALIGN(nr_cpu_ids, bpw) / bpw;
1466 bitmap->map = kzalloc_node(num_maps * sizeof(struct blk_align_bitmap),
1471 bitmap->map_size = num_maps;
1474 for (i = 0; i < num_maps; i++) {
1475 bitmap->map[i].depth = min(total, bitmap->bits_per_word);
1476 total -= bitmap->map[i].depth;
1482 static int blk_mq_hctx_cpu_offline(struct blk_mq_hw_ctx *hctx, int cpu)
1484 struct request_queue *q = hctx->queue;
1485 struct blk_mq_ctx *ctx;
1489 * Move ctx entries to new CPU, if this one is going away.
1491 ctx = __blk_mq_get_ctx(q, cpu);
1493 spin_lock(&ctx->lock);
1494 if (!list_empty(&ctx->rq_list)) {
1495 list_splice_init(&ctx->rq_list, &tmp);
1496 blk_mq_hctx_clear_pending(hctx, ctx);
1498 spin_unlock(&ctx->lock);
1500 if (list_empty(&tmp))
1503 ctx = blk_mq_get_ctx(q);
1504 spin_lock(&ctx->lock);
1506 while (!list_empty(&tmp)) {
1509 rq = list_first_entry(&tmp, struct request, queuelist);
1511 list_move_tail(&rq->queuelist, &ctx->rq_list);
1514 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1515 blk_mq_hctx_mark_pending(hctx, ctx);
1517 spin_unlock(&ctx->lock);
1519 blk_mq_run_hw_queue(hctx, true);
1520 blk_mq_put_ctx(ctx);
1524 static int blk_mq_hctx_cpu_online(struct blk_mq_hw_ctx *hctx, int cpu)
1526 struct request_queue *q = hctx->queue;
1527 struct blk_mq_tag_set *set = q->tag_set;
1529 if (set->tags[hctx->queue_num])
1532 set->tags[hctx->queue_num] = blk_mq_init_rq_map(set, hctx->queue_num);
1533 if (!set->tags[hctx->queue_num])
1536 hctx->tags = set->tags[hctx->queue_num];
1540 static int blk_mq_hctx_notify(void *data, unsigned long action,
1543 struct blk_mq_hw_ctx *hctx = data;
1545 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN)
1546 return blk_mq_hctx_cpu_offline(hctx, cpu);
1547 else if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN)
1548 return blk_mq_hctx_cpu_online(hctx, cpu);
1553 static void blk_mq_exit_hctx(struct request_queue *q,
1554 struct blk_mq_tag_set *set,
1555 struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
1557 unsigned flush_start_tag = set->queue_depth;
1559 blk_mq_tag_idle(hctx);
1561 if (set->ops->exit_request)
1562 set->ops->exit_request(set->driver_data,
1563 hctx->fq->flush_rq, hctx_idx,
1564 flush_start_tag + hctx_idx);
1566 if (set->ops->exit_hctx)
1567 set->ops->exit_hctx(hctx, hctx_idx);
1569 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
1570 blk_free_flush_queue(hctx->fq);
1572 blk_mq_free_bitmap(&hctx->ctx_map);
1575 static void blk_mq_exit_hw_queues(struct request_queue *q,
1576 struct blk_mq_tag_set *set, int nr_queue)
1578 struct blk_mq_hw_ctx *hctx;
1581 queue_for_each_hw_ctx(q, hctx, i) {
1584 blk_mq_exit_hctx(q, set, hctx, i);
1588 static void blk_mq_free_hw_queues(struct request_queue *q,
1589 struct blk_mq_tag_set *set)
1591 struct blk_mq_hw_ctx *hctx;
1594 queue_for_each_hw_ctx(q, hctx, i) {
1595 free_cpumask_var(hctx->cpumask);
1600 static int blk_mq_init_hctx(struct request_queue *q,
1601 struct blk_mq_tag_set *set,
1602 struct blk_mq_hw_ctx *hctx, unsigned hctx_idx)
1605 unsigned flush_start_tag = set->queue_depth;
1607 node = hctx->numa_node;
1608 if (node == NUMA_NO_NODE)
1609 node = hctx->numa_node = set->numa_node;
1611 INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn);
1612 INIT_DELAYED_WORK(&hctx->delay_work, blk_mq_delay_work_fn);
1613 spin_lock_init(&hctx->lock);
1614 INIT_LIST_HEAD(&hctx->dispatch);
1616 hctx->queue_num = hctx_idx;
1617 hctx->flags = set->flags;
1618 hctx->cmd_size = set->cmd_size;
1620 blk_mq_init_cpu_notifier(&hctx->cpu_notifier,
1621 blk_mq_hctx_notify, hctx);
1622 blk_mq_register_cpu_notifier(&hctx->cpu_notifier);
1624 hctx->tags = set->tags[hctx_idx];
1627 * Allocate space for all possible cpus to avoid allocation at
1630 hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *),
1633 goto unregister_cpu_notifier;
1635 if (blk_mq_alloc_bitmap(&hctx->ctx_map, node))
1640 if (set->ops->init_hctx &&
1641 set->ops->init_hctx(hctx, set->driver_data, hctx_idx))
1644 hctx->fq = blk_alloc_flush_queue(q, hctx->numa_node, set->cmd_size);
1648 if (set->ops->init_request &&
1649 set->ops->init_request(set->driver_data,
1650 hctx->fq->flush_rq, hctx_idx,
1651 flush_start_tag + hctx_idx, node))
1659 if (set->ops->exit_hctx)
1660 set->ops->exit_hctx(hctx, hctx_idx);
1662 blk_mq_free_bitmap(&hctx->ctx_map);
1665 unregister_cpu_notifier:
1666 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
1671 static int blk_mq_init_hw_queues(struct request_queue *q,
1672 struct blk_mq_tag_set *set)
1674 struct blk_mq_hw_ctx *hctx;
1678 * Initialize hardware queues
1680 queue_for_each_hw_ctx(q, hctx, i) {
1681 if (blk_mq_init_hctx(q, set, hctx, i))
1685 if (i == q->nr_hw_queues)
1691 blk_mq_exit_hw_queues(q, set, i);
1696 static void blk_mq_init_cpu_queues(struct request_queue *q,
1697 unsigned int nr_hw_queues)
1701 for_each_possible_cpu(i) {
1702 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
1703 struct blk_mq_hw_ctx *hctx;
1705 memset(__ctx, 0, sizeof(*__ctx));
1707 spin_lock_init(&__ctx->lock);
1708 INIT_LIST_HEAD(&__ctx->rq_list);
1711 /* If the cpu isn't online, the cpu is mapped to first hctx */
1715 hctx = q->mq_ops->map_queue(q, i);
1716 cpumask_set_cpu(i, hctx->cpumask);
1720 * Set local node, IFF we have more than one hw queue. If
1721 * not, we remain on the home node of the device
1723 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
1724 hctx->numa_node = cpu_to_node(i);
1728 static void blk_mq_map_swqueue(struct request_queue *q)
1731 struct blk_mq_hw_ctx *hctx;
1732 struct blk_mq_ctx *ctx;
1734 queue_for_each_hw_ctx(q, hctx, i) {
1735 cpumask_clear(hctx->cpumask);
1740 * Map software to hardware queues
1742 queue_for_each_ctx(q, ctx, i) {
1743 /* If the cpu isn't online, the cpu is mapped to first hctx */
1747 hctx = q->mq_ops->map_queue(q, i);
1748 cpumask_set_cpu(i, hctx->cpumask);
1749 ctx->index_hw = hctx->nr_ctx;
1750 hctx->ctxs[hctx->nr_ctx++] = ctx;
1753 queue_for_each_hw_ctx(q, hctx, i) {
1755 * If no software queues are mapped to this hardware queue,
1756 * disable it and free the request entries.
1758 if (!hctx->nr_ctx) {
1759 struct blk_mq_tag_set *set = q->tag_set;
1762 blk_mq_free_rq_map(set, set->tags[i], i);
1763 set->tags[i] = NULL;
1770 * Initialize batch roundrobin counts
1772 hctx->next_cpu = cpumask_first(hctx->cpumask);
1773 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
1777 static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set *set)
1779 struct blk_mq_hw_ctx *hctx;
1780 struct request_queue *q;
1784 if (set->tag_list.next == set->tag_list.prev)
1789 list_for_each_entry(q, &set->tag_list, tag_set_list) {
1790 blk_mq_freeze_queue(q);
1792 queue_for_each_hw_ctx(q, hctx, i) {
1794 hctx->flags |= BLK_MQ_F_TAG_SHARED;
1796 hctx->flags &= ~BLK_MQ_F_TAG_SHARED;
1798 blk_mq_unfreeze_queue(q);
1802 static void blk_mq_del_queue_tag_set(struct request_queue *q)
1804 struct blk_mq_tag_set *set = q->tag_set;
1806 mutex_lock(&set->tag_list_lock);
1807 list_del_init(&q->tag_set_list);
1808 blk_mq_update_tag_set_depth(set);
1809 mutex_unlock(&set->tag_list_lock);
1812 static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set,
1813 struct request_queue *q)
1817 mutex_lock(&set->tag_list_lock);
1818 list_add_tail(&q->tag_set_list, &set->tag_list);
1819 blk_mq_update_tag_set_depth(set);
1820 mutex_unlock(&set->tag_list_lock);
1823 struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
1825 struct blk_mq_hw_ctx **hctxs;
1826 struct blk_mq_ctx __percpu *ctx;
1827 struct request_queue *q;
1831 ctx = alloc_percpu(struct blk_mq_ctx);
1833 return ERR_PTR(-ENOMEM);
1835 hctxs = kmalloc_node(set->nr_hw_queues * sizeof(*hctxs), GFP_KERNEL,
1841 map = blk_mq_make_queue_map(set);
1845 for (i = 0; i < set->nr_hw_queues; i++) {
1846 int node = blk_mq_hw_queue_to_node(map, i);
1848 hctxs[i] = kzalloc_node(sizeof(struct blk_mq_hw_ctx),
1853 if (!zalloc_cpumask_var_node(&hctxs[i]->cpumask, GFP_KERNEL,
1857 atomic_set(&hctxs[i]->nr_active, 0);
1858 hctxs[i]->numa_node = node;
1859 hctxs[i]->queue_num = i;
1862 q = blk_alloc_queue_node(GFP_KERNEL, set->numa_node);
1867 * Init percpu_ref in atomic mode so that it's faster to shutdown.
1868 * See blk_register_queue() for details.
1870 if (percpu_ref_init(&q->mq_usage_counter, blk_mq_usage_counter_release,
1871 PERCPU_REF_INIT_ATOMIC, GFP_KERNEL))
1874 setup_timer(&q->timeout, blk_mq_rq_timer, (unsigned long) q);
1875 blk_queue_rq_timeout(q, 30000);
1877 q->nr_queues = nr_cpu_ids;
1878 q->nr_hw_queues = set->nr_hw_queues;
1882 q->queue_hw_ctx = hctxs;
1884 q->mq_ops = set->ops;
1885 q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
1887 if (!(set->flags & BLK_MQ_F_SG_MERGE))
1888 q->queue_flags |= 1 << QUEUE_FLAG_NO_SG_MERGE;
1890 q->sg_reserved_size = INT_MAX;
1892 INIT_WORK(&q->requeue_work, blk_mq_requeue_work);
1893 INIT_LIST_HEAD(&q->requeue_list);
1894 spin_lock_init(&q->requeue_lock);
1896 if (q->nr_hw_queues > 1)
1897 blk_queue_make_request(q, blk_mq_make_request);
1899 blk_queue_make_request(q, blk_sq_make_request);
1902 blk_queue_rq_timeout(q, set->timeout);
1905 * Do this after blk_queue_make_request() overrides it...
1907 q->nr_requests = set->queue_depth;
1909 if (set->ops->complete)
1910 blk_queue_softirq_done(q, set->ops->complete);
1912 blk_mq_init_cpu_queues(q, set->nr_hw_queues);
1914 if (blk_mq_init_hw_queues(q, set))
1917 mutex_lock(&all_q_mutex);
1918 list_add_tail(&q->all_q_node, &all_q_list);
1919 mutex_unlock(&all_q_mutex);
1921 blk_mq_add_queue_tag_set(set, q);
1923 blk_mq_map_swqueue(q);
1928 blk_cleanup_queue(q);
1931 for (i = 0; i < set->nr_hw_queues; i++) {
1934 free_cpumask_var(hctxs[i]->cpumask);
1941 return ERR_PTR(-ENOMEM);
1943 EXPORT_SYMBOL(blk_mq_init_queue);
1945 void blk_mq_free_queue(struct request_queue *q)
1947 struct blk_mq_tag_set *set = q->tag_set;
1949 blk_mq_del_queue_tag_set(q);
1951 blk_mq_exit_hw_queues(q, set, set->nr_hw_queues);
1952 blk_mq_free_hw_queues(q, set);
1954 percpu_ref_exit(&q->mq_usage_counter);
1956 free_percpu(q->queue_ctx);
1957 kfree(q->queue_hw_ctx);
1960 q->queue_ctx = NULL;
1961 q->queue_hw_ctx = NULL;
1964 mutex_lock(&all_q_mutex);
1965 list_del_init(&q->all_q_node);
1966 mutex_unlock(&all_q_mutex);
1969 /* Basically redo blk_mq_init_queue with queue frozen */
1970 static void blk_mq_queue_reinit(struct request_queue *q)
1972 WARN_ON_ONCE(!q->mq_freeze_depth);
1974 blk_mq_sysfs_unregister(q);
1976 blk_mq_update_queue_map(q->mq_map, q->nr_hw_queues);
1979 * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
1980 * we should change hctx numa_node according to new topology (this
1981 * involves free and re-allocate memory, worthy doing?)
1984 blk_mq_map_swqueue(q);
1986 blk_mq_sysfs_register(q);
1989 static int blk_mq_queue_reinit_notify(struct notifier_block *nb,
1990 unsigned long action, void *hcpu)
1992 struct request_queue *q;
1995 * Before new mappings are established, hotadded cpu might already
1996 * start handling requests. This doesn't break anything as we map
1997 * offline CPUs to first hardware queue. We will re-init the queue
1998 * below to get optimal settings.
2000 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN &&
2001 action != CPU_ONLINE && action != CPU_ONLINE_FROZEN)
2004 mutex_lock(&all_q_mutex);
2007 * We need to freeze and reinit all existing queues. Freezing
2008 * involves synchronous wait for an RCU grace period and doing it
2009 * one by one may take a long time. Start freezing all queues in
2010 * one swoop and then wait for the completions so that freezing can
2011 * take place in parallel.
2013 list_for_each_entry(q, &all_q_list, all_q_node)
2014 blk_mq_freeze_queue_start(q);
2015 list_for_each_entry(q, &all_q_list, all_q_node)
2016 blk_mq_freeze_queue_wait(q);
2018 list_for_each_entry(q, &all_q_list, all_q_node)
2019 blk_mq_queue_reinit(q);
2021 list_for_each_entry(q, &all_q_list, all_q_node)
2022 blk_mq_unfreeze_queue(q);
2024 mutex_unlock(&all_q_mutex);
2028 static int __blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set)
2032 for (i = 0; i < set->nr_hw_queues; i++) {
2033 set->tags[i] = blk_mq_init_rq_map(set, i);
2042 blk_mq_free_rq_map(set, set->tags[i], i);
2048 * Allocate the request maps associated with this tag_set. Note that this
2049 * may reduce the depth asked for, if memory is tight. set->queue_depth
2050 * will be updated to reflect the allocated depth.
2052 static int blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set)
2057 depth = set->queue_depth;
2059 err = __blk_mq_alloc_rq_maps(set);
2063 set->queue_depth >>= 1;
2064 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) {
2068 } while (set->queue_depth);
2070 if (!set->queue_depth || err) {
2071 pr_err("blk-mq: failed to allocate request map\n");
2075 if (depth != set->queue_depth)
2076 pr_info("blk-mq: reduced tag depth (%u -> %u)\n",
2077 depth, set->queue_depth);
2083 * Alloc a tag set to be associated with one or more request queues.
2084 * May fail with EINVAL for various error conditions. May adjust the
2085 * requested depth down, if if it too large. In that case, the set
2086 * value will be stored in set->queue_depth.
2088 int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
2090 BUILD_BUG_ON(BLK_MQ_MAX_DEPTH > 1 << BLK_MQ_UNIQUE_TAG_BITS);
2092 if (!set->nr_hw_queues)
2094 if (!set->queue_depth)
2096 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN)
2099 if (!set->nr_hw_queues || !set->ops->queue_rq || !set->ops->map_queue)
2102 if (set->queue_depth > BLK_MQ_MAX_DEPTH) {
2103 pr_info("blk-mq: reduced tag depth to %u\n",
2105 set->queue_depth = BLK_MQ_MAX_DEPTH;
2109 * If a crashdump is active, then we are potentially in a very
2110 * memory constrained environment. Limit us to 1 queue and
2111 * 64 tags to prevent using too much memory.
2113 if (is_kdump_kernel()) {
2114 set->nr_hw_queues = 1;
2115 set->queue_depth = min(64U, set->queue_depth);
2118 set->tags = kmalloc_node(set->nr_hw_queues *
2119 sizeof(struct blk_mq_tags *),
2120 GFP_KERNEL, set->numa_node);
2124 if (blk_mq_alloc_rq_maps(set))
2127 mutex_init(&set->tag_list_lock);
2128 INIT_LIST_HEAD(&set->tag_list);
2136 EXPORT_SYMBOL(blk_mq_alloc_tag_set);
2138 void blk_mq_free_tag_set(struct blk_mq_tag_set *set)
2142 for (i = 0; i < set->nr_hw_queues; i++) {
2144 blk_mq_free_rq_map(set, set->tags[i], i);
2150 EXPORT_SYMBOL(blk_mq_free_tag_set);
2152 int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr)
2154 struct blk_mq_tag_set *set = q->tag_set;
2155 struct blk_mq_hw_ctx *hctx;
2158 if (!set || nr > set->queue_depth)
2162 queue_for_each_hw_ctx(q, hctx, i) {
2163 ret = blk_mq_tag_update_depth(hctx->tags, nr);
2169 q->nr_requests = nr;
2174 void blk_mq_disable_hotplug(void)
2176 mutex_lock(&all_q_mutex);
2179 void blk_mq_enable_hotplug(void)
2181 mutex_unlock(&all_q_mutex);
2184 static int __init blk_mq_init(void)
2188 hotcpu_notifier(blk_mq_queue_reinit_notify, 0);
2192 subsys_initcall(blk_mq_init);