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>
12 #include <linux/kmemleak.h>
14 #include <linux/init.h>
15 #include <linux/slab.h>
16 #include <linux/workqueue.h>
17 #include <linux/smp.h>
18 #include <linux/llist.h>
19 #include <linux/list_sort.h>
20 #include <linux/cpu.h>
21 #include <linux/cache.h>
22 #include <linux/sched/sysctl.h>
23 #include <linux/delay.h>
24 #include <linux/crash_dump.h>
25 #include <linux/prefetch.h>
27 #include <trace/events/block.h>
29 #include <linux/blk-mq.h>
32 #include "blk-mq-tag.h"
34 static DEFINE_MUTEX(all_q_mutex);
35 static LIST_HEAD(all_q_list);
37 static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx);
40 * Check if any of the ctx's have pending work in this hardware queue
42 static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx)
46 for (i = 0; i < hctx->ctx_map.size; i++)
47 if (hctx->ctx_map.map[i].word)
53 static inline struct blk_align_bitmap *get_bm(struct blk_mq_hw_ctx *hctx,
54 struct blk_mq_ctx *ctx)
56 return &hctx->ctx_map.map[ctx->index_hw / hctx->ctx_map.bits_per_word];
59 #define CTX_TO_BIT(hctx, ctx) \
60 ((ctx)->index_hw & ((hctx)->ctx_map.bits_per_word - 1))
63 * Mark this ctx as having pending work in this hardware queue
65 static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx,
66 struct blk_mq_ctx *ctx)
68 struct blk_align_bitmap *bm = get_bm(hctx, ctx);
70 if (!test_bit(CTX_TO_BIT(hctx, ctx), &bm->word))
71 set_bit(CTX_TO_BIT(hctx, ctx), &bm->word);
74 static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx *hctx,
75 struct blk_mq_ctx *ctx)
77 struct blk_align_bitmap *bm = get_bm(hctx, ctx);
79 clear_bit(CTX_TO_BIT(hctx, ctx), &bm->word);
82 void blk_mq_freeze_queue_start(struct request_queue *q)
86 freeze_depth = atomic_inc_return(&q->mq_freeze_depth);
87 if (freeze_depth == 1) {
88 percpu_ref_kill(&q->q_usage_counter);
89 blk_mq_run_hw_queues(q, false);
92 EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_start);
94 static void blk_mq_freeze_queue_wait(struct request_queue *q)
96 wait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->q_usage_counter));
100 * Guarantee no request is in use, so we can change any data structure of
101 * the queue afterward.
103 void blk_freeze_queue(struct request_queue *q)
106 * In the !blk_mq case we are only calling this to kill the
107 * q_usage_counter, otherwise this increases the freeze depth
108 * and waits for it to return to zero. For this reason there is
109 * no blk_unfreeze_queue(), and blk_freeze_queue() is not
110 * exported to drivers as the only user for unfreeze is blk_mq.
112 blk_mq_freeze_queue_start(q);
113 blk_mq_freeze_queue_wait(q);
116 void blk_mq_freeze_queue(struct request_queue *q)
119 * ...just an alias to keep freeze and unfreeze actions balanced
120 * in the blk_mq_* namespace
124 EXPORT_SYMBOL_GPL(blk_mq_freeze_queue);
126 void blk_mq_unfreeze_queue(struct request_queue *q)
130 freeze_depth = atomic_dec_return(&q->mq_freeze_depth);
131 WARN_ON_ONCE(freeze_depth < 0);
133 percpu_ref_reinit(&q->q_usage_counter);
134 wake_up_all(&q->mq_freeze_wq);
137 EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue);
139 void blk_mq_wake_waiters(struct request_queue *q)
141 struct blk_mq_hw_ctx *hctx;
144 queue_for_each_hw_ctx(q, hctx, i)
145 if (blk_mq_hw_queue_mapped(hctx))
146 blk_mq_tag_wakeup_all(hctx->tags, true);
149 * If we are called because the queue has now been marked as
150 * dying, we need to ensure that processes currently waiting on
151 * the queue are notified as well.
153 wake_up_all(&q->mq_freeze_wq);
156 bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx)
158 return blk_mq_has_free_tags(hctx->tags);
160 EXPORT_SYMBOL(blk_mq_can_queue);
162 static void blk_mq_rq_ctx_init(struct request_queue *q, struct blk_mq_ctx *ctx,
163 struct request *rq, int op,
164 unsigned int op_flags)
166 if (blk_queue_io_stat(q))
167 op_flags |= REQ_IO_STAT;
169 INIT_LIST_HEAD(&rq->queuelist);
170 /* csd/requeue_work/fifo_time is initialized before use */
173 req_set_op_attrs(rq, op, op_flags);
174 /* do not touch atomic flags, it needs atomic ops against the timer */
176 INIT_HLIST_NODE(&rq->hash);
177 RB_CLEAR_NODE(&rq->rb_node);
180 rq->start_time = jiffies;
181 #ifdef CONFIG_BLK_CGROUP
183 set_start_time_ns(rq);
184 rq->io_start_time_ns = 0;
186 rq->nr_phys_segments = 0;
187 #if defined(CONFIG_BLK_DEV_INTEGRITY)
188 rq->nr_integrity_segments = 0;
191 /* tag was already set */
201 INIT_LIST_HEAD(&rq->timeout_list);
205 rq->end_io_data = NULL;
208 ctx->rq_dispatched[rw_is_sync(op, op_flags)]++;
211 static struct request *
212 __blk_mq_alloc_request(struct blk_mq_alloc_data *data, int op, int op_flags)
217 tag = blk_mq_get_tag(data);
218 if (tag != BLK_MQ_TAG_FAIL) {
219 rq = data->hctx->tags->rqs[tag];
221 if (blk_mq_tag_busy(data->hctx)) {
222 rq->cmd_flags = REQ_MQ_INFLIGHT;
223 atomic_inc(&data->hctx->nr_active);
227 blk_mq_rq_ctx_init(data->q, data->ctx, rq, op, op_flags);
234 struct request *blk_mq_alloc_request(struct request_queue *q, int rw,
237 struct blk_mq_ctx *ctx;
238 struct blk_mq_hw_ctx *hctx;
240 struct blk_mq_alloc_data alloc_data;
243 ret = blk_queue_enter(q, flags & BLK_MQ_REQ_NOWAIT);
247 ctx = blk_mq_get_ctx(q);
248 hctx = blk_mq_map_queue(q, ctx->cpu);
249 blk_mq_set_alloc_data(&alloc_data, q, flags, ctx, hctx);
251 rq = __blk_mq_alloc_request(&alloc_data, rw, 0);
252 if (!rq && !(flags & BLK_MQ_REQ_NOWAIT)) {
253 __blk_mq_run_hw_queue(hctx);
256 ctx = blk_mq_get_ctx(q);
257 hctx = blk_mq_map_queue(q, ctx->cpu);
258 blk_mq_set_alloc_data(&alloc_data, q, flags, ctx, hctx);
259 rq = __blk_mq_alloc_request(&alloc_data, rw, 0);
260 ctx = alloc_data.ctx;
265 return ERR_PTR(-EWOULDBLOCK);
269 rq->__sector = (sector_t) -1;
270 rq->bio = rq->biotail = NULL;
273 EXPORT_SYMBOL(blk_mq_alloc_request);
275 struct request *blk_mq_alloc_request_hctx(struct request_queue *q, int rw,
276 unsigned int flags, unsigned int hctx_idx)
278 struct blk_mq_hw_ctx *hctx;
279 struct blk_mq_ctx *ctx;
281 struct blk_mq_alloc_data alloc_data;
285 * If the tag allocator sleeps we could get an allocation for a
286 * different hardware context. No need to complicate the low level
287 * allocator for this for the rare use case of a command tied to
290 if (WARN_ON_ONCE(!(flags & BLK_MQ_REQ_NOWAIT)))
291 return ERR_PTR(-EINVAL);
293 if (hctx_idx >= q->nr_hw_queues)
294 return ERR_PTR(-EIO);
296 ret = blk_queue_enter(q, true);
300 hctx = q->queue_hw_ctx[hctx_idx];
301 ctx = __blk_mq_get_ctx(q, cpumask_first(hctx->cpumask));
303 blk_mq_set_alloc_data(&alloc_data, q, flags, ctx, hctx);
304 rq = __blk_mq_alloc_request(&alloc_data, rw, 0);
307 return ERR_PTR(-EWOULDBLOCK);
312 EXPORT_SYMBOL_GPL(blk_mq_alloc_request_hctx);
314 static void __blk_mq_free_request(struct blk_mq_hw_ctx *hctx,
315 struct blk_mq_ctx *ctx, struct request *rq)
317 const int tag = rq->tag;
318 struct request_queue *q = rq->q;
320 if (rq->cmd_flags & REQ_MQ_INFLIGHT)
321 atomic_dec(&hctx->nr_active);
324 clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
325 blk_mq_put_tag(hctx, tag, &ctx->last_tag);
329 void blk_mq_free_hctx_request(struct blk_mq_hw_ctx *hctx, struct request *rq)
331 struct blk_mq_ctx *ctx = rq->mq_ctx;
333 ctx->rq_completed[rq_is_sync(rq)]++;
334 __blk_mq_free_request(hctx, ctx, rq);
337 EXPORT_SYMBOL_GPL(blk_mq_free_hctx_request);
339 void blk_mq_free_request(struct request *rq)
341 blk_mq_free_hctx_request(blk_mq_map_queue(rq->q, rq->mq_ctx->cpu), rq);
343 EXPORT_SYMBOL_GPL(blk_mq_free_request);
345 inline void __blk_mq_end_request(struct request *rq, int error)
347 blk_account_io_done(rq);
350 rq->end_io(rq, error);
352 if (unlikely(blk_bidi_rq(rq)))
353 blk_mq_free_request(rq->next_rq);
354 blk_mq_free_request(rq);
357 EXPORT_SYMBOL(__blk_mq_end_request);
359 void blk_mq_end_request(struct request *rq, int error)
361 if (blk_update_request(rq, error, blk_rq_bytes(rq)))
363 __blk_mq_end_request(rq, error);
365 EXPORT_SYMBOL(blk_mq_end_request);
367 static void __blk_mq_complete_request_remote(void *data)
369 struct request *rq = data;
371 rq->q->softirq_done_fn(rq);
374 static void blk_mq_ipi_complete_request(struct request *rq)
376 struct blk_mq_ctx *ctx = rq->mq_ctx;
380 if (!test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags)) {
381 rq->q->softirq_done_fn(rq);
386 if (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags))
387 shared = cpus_share_cache(cpu, ctx->cpu);
389 if (cpu != ctx->cpu && !shared && cpu_online(ctx->cpu)) {
390 rq->csd.func = __blk_mq_complete_request_remote;
393 smp_call_function_single_async(ctx->cpu, &rq->csd);
395 rq->q->softirq_done_fn(rq);
400 static void __blk_mq_complete_request(struct request *rq)
402 struct request_queue *q = rq->q;
404 if (!q->softirq_done_fn)
405 blk_mq_end_request(rq, rq->errors);
407 blk_mq_ipi_complete_request(rq);
411 * blk_mq_complete_request - end I/O on a request
412 * @rq: the request being processed
415 * Ends all I/O on a request. It does not handle partial completions.
416 * The actual completion happens out-of-order, through a IPI handler.
418 void blk_mq_complete_request(struct request *rq, int error)
420 struct request_queue *q = rq->q;
422 if (unlikely(blk_should_fake_timeout(q)))
424 if (!blk_mark_rq_complete(rq)) {
426 __blk_mq_complete_request(rq);
429 EXPORT_SYMBOL(blk_mq_complete_request);
431 int blk_mq_request_started(struct request *rq)
433 return test_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
435 EXPORT_SYMBOL_GPL(blk_mq_request_started);
437 void blk_mq_start_request(struct request *rq)
439 struct request_queue *q = rq->q;
441 trace_block_rq_issue(q, rq);
443 rq->resid_len = blk_rq_bytes(rq);
444 if (unlikely(blk_bidi_rq(rq)))
445 rq->next_rq->resid_len = blk_rq_bytes(rq->next_rq);
450 * Ensure that ->deadline is visible before set the started
451 * flag and clear the completed flag.
453 smp_mb__before_atomic();
456 * Mark us as started and clear complete. Complete might have been
457 * set if requeue raced with timeout, which then marked it as
458 * complete. So be sure to clear complete again when we start
459 * the request, otherwise we'll ignore the completion event.
461 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
462 set_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
463 if (test_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags))
464 clear_bit(REQ_ATOM_COMPLETE, &rq->atomic_flags);
466 if (q->dma_drain_size && blk_rq_bytes(rq)) {
468 * Make sure space for the drain appears. We know we can do
469 * this because max_hw_segments has been adjusted to be one
470 * fewer than the device can handle.
472 rq->nr_phys_segments++;
475 EXPORT_SYMBOL(blk_mq_start_request);
477 static void __blk_mq_requeue_request(struct request *rq)
479 struct request_queue *q = rq->q;
481 trace_block_rq_requeue(q, rq);
483 if (test_and_clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags)) {
484 if (q->dma_drain_size && blk_rq_bytes(rq))
485 rq->nr_phys_segments--;
489 void blk_mq_requeue_request(struct request *rq)
491 __blk_mq_requeue_request(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.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);
526 * Use the start variant of queue running here, so that running
527 * the requeue work will kick stopped queues.
529 blk_mq_start_hw_queues(q);
532 void blk_mq_add_to_requeue_list(struct request *rq, bool at_head)
534 struct request_queue *q = rq->q;
538 * We abuse this flag that is otherwise used by the I/O scheduler to
539 * request head insertation from the workqueue.
541 BUG_ON(rq->cmd_flags & REQ_SOFTBARRIER);
543 spin_lock_irqsave(&q->requeue_lock, flags);
545 rq->cmd_flags |= REQ_SOFTBARRIER;
546 list_add(&rq->queuelist, &q->requeue_list);
548 list_add_tail(&rq->queuelist, &q->requeue_list);
550 spin_unlock_irqrestore(&q->requeue_lock, flags);
552 EXPORT_SYMBOL(blk_mq_add_to_requeue_list);
554 void blk_mq_cancel_requeue_work(struct request_queue *q)
556 cancel_delayed_work_sync(&q->requeue_work);
558 EXPORT_SYMBOL_GPL(blk_mq_cancel_requeue_work);
560 void blk_mq_kick_requeue_list(struct request_queue *q)
562 kblockd_schedule_delayed_work(&q->requeue_work, 0);
564 EXPORT_SYMBOL(blk_mq_kick_requeue_list);
566 void blk_mq_delay_kick_requeue_list(struct request_queue *q,
569 kblockd_schedule_delayed_work(&q->requeue_work,
570 msecs_to_jiffies(msecs));
572 EXPORT_SYMBOL(blk_mq_delay_kick_requeue_list);
574 void blk_mq_abort_requeue_list(struct request_queue *q)
579 spin_lock_irqsave(&q->requeue_lock, flags);
580 list_splice_init(&q->requeue_list, &rq_list);
581 spin_unlock_irqrestore(&q->requeue_lock, flags);
583 while (!list_empty(&rq_list)) {
586 rq = list_first_entry(&rq_list, struct request, queuelist);
587 list_del_init(&rq->queuelist);
589 blk_mq_end_request(rq, rq->errors);
592 EXPORT_SYMBOL(blk_mq_abort_requeue_list);
594 struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag)
596 if (tag < tags->nr_tags) {
597 prefetch(tags->rqs[tag]);
598 return tags->rqs[tag];
603 EXPORT_SYMBOL(blk_mq_tag_to_rq);
605 struct blk_mq_timeout_data {
607 unsigned int next_set;
610 void blk_mq_rq_timed_out(struct request *req, bool reserved)
612 struct blk_mq_ops *ops = req->q->mq_ops;
613 enum blk_eh_timer_return ret = BLK_EH_RESET_TIMER;
616 * We know that complete is set at this point. If STARTED isn't set
617 * anymore, then the request isn't active and the "timeout" should
618 * just be ignored. This can happen due to the bitflag ordering.
619 * Timeout first checks if STARTED is set, and if it is, assumes
620 * the request is active. But if we race with completion, then
621 * we both flags will get cleared. So check here again, and ignore
622 * a timeout event with a request that isn't active.
624 if (!test_bit(REQ_ATOM_STARTED, &req->atomic_flags))
628 ret = ops->timeout(req, reserved);
632 __blk_mq_complete_request(req);
634 case BLK_EH_RESET_TIMER:
636 blk_clear_rq_complete(req);
638 case BLK_EH_NOT_HANDLED:
641 printk(KERN_ERR "block: bad eh return: %d\n", ret);
646 static void blk_mq_check_expired(struct blk_mq_hw_ctx *hctx,
647 struct request *rq, void *priv, bool reserved)
649 struct blk_mq_timeout_data *data = priv;
651 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags)) {
653 * If a request wasn't started before the queue was
654 * marked dying, kill it here or it'll go unnoticed.
656 if (unlikely(blk_queue_dying(rq->q))) {
658 blk_mq_end_request(rq, rq->errors);
663 if (time_after_eq(jiffies, rq->deadline)) {
664 if (!blk_mark_rq_complete(rq))
665 blk_mq_rq_timed_out(rq, reserved);
666 } else if (!data->next_set || time_after(data->next, rq->deadline)) {
667 data->next = rq->deadline;
672 static void blk_mq_timeout_work(struct work_struct *work)
674 struct request_queue *q =
675 container_of(work, struct request_queue, timeout_work);
676 struct blk_mq_timeout_data data = {
682 /* A deadlock might occur if a request is stuck requiring a
683 * timeout at the same time a queue freeze is waiting
684 * completion, since the timeout code would not be able to
685 * acquire the queue reference here.
687 * That's why we don't use blk_queue_enter here; instead, we use
688 * percpu_ref_tryget directly, because we need to be able to
689 * obtain a reference even in the short window between the queue
690 * starting to freeze, by dropping the first reference in
691 * blk_mq_freeze_queue_start, and the moment the last request is
692 * consumed, marked by the instant q_usage_counter reaches
695 if (!percpu_ref_tryget(&q->q_usage_counter))
698 blk_mq_queue_tag_busy_iter(q, blk_mq_check_expired, &data);
701 data.next = blk_rq_timeout(round_jiffies_up(data.next));
702 mod_timer(&q->timeout, data.next);
704 struct blk_mq_hw_ctx *hctx;
706 queue_for_each_hw_ctx(q, hctx, i) {
707 /* the hctx may be unmapped, so check it here */
708 if (blk_mq_hw_queue_mapped(hctx))
709 blk_mq_tag_idle(hctx);
716 * Reverse check our software queue for entries that we could potentially
717 * merge with. Currently includes a hand-wavy stop count of 8, to not spend
718 * too much time checking for merges.
720 static bool blk_mq_attempt_merge(struct request_queue *q,
721 struct blk_mq_ctx *ctx, struct bio *bio)
726 list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) {
732 if (!blk_rq_merge_ok(rq, bio))
735 el_ret = blk_try_merge(rq, bio);
736 if (el_ret == ELEVATOR_BACK_MERGE) {
737 if (bio_attempt_back_merge(q, rq, bio)) {
742 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
743 if (bio_attempt_front_merge(q, rq, bio)) {
755 * Process software queues that have been marked busy, splicing them
756 * to the for-dispatch
758 static void flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list)
760 struct blk_mq_ctx *ctx;
763 for (i = 0; i < hctx->ctx_map.size; i++) {
764 struct blk_align_bitmap *bm = &hctx->ctx_map.map[i];
765 unsigned int off, bit;
771 off = i * hctx->ctx_map.bits_per_word;
773 bit = find_next_bit(&bm->word, bm->depth, bit);
774 if (bit >= bm->depth)
777 ctx = hctx->ctxs[bit + off];
778 clear_bit(bit, &bm->word);
779 spin_lock(&ctx->lock);
780 list_splice_tail_init(&ctx->rq_list, list);
781 spin_unlock(&ctx->lock);
789 * Run this hardware queue, pulling any software queues mapped to it in.
790 * Note that this function currently has various problems around ordering
791 * of IO. In particular, we'd like FIFO behaviour on handling existing
792 * items on the hctx->dispatch list. Ignore that for now.
794 static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
796 struct request_queue *q = hctx->queue;
799 LIST_HEAD(driver_list);
800 struct list_head *dptr;
803 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
806 WARN_ON(!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask) &&
807 cpu_online(hctx->next_cpu));
812 * Touch any software queue that has pending entries.
814 flush_busy_ctxs(hctx, &rq_list);
817 * If we have previous entries on our dispatch list, grab them
818 * and stuff them at the front for more fair dispatch.
820 if (!list_empty_careful(&hctx->dispatch)) {
821 spin_lock(&hctx->lock);
822 if (!list_empty(&hctx->dispatch))
823 list_splice_init(&hctx->dispatch, &rq_list);
824 spin_unlock(&hctx->lock);
828 * Start off with dptr being NULL, so we start the first request
829 * immediately, even if we have more pending.
834 * Now process all the entries, sending them to the driver.
837 while (!list_empty(&rq_list)) {
838 struct blk_mq_queue_data bd;
841 rq = list_first_entry(&rq_list, struct request, queuelist);
842 list_del_init(&rq->queuelist);
846 bd.last = list_empty(&rq_list);
848 ret = q->mq_ops->queue_rq(hctx, &bd);
850 case BLK_MQ_RQ_QUEUE_OK:
853 case BLK_MQ_RQ_QUEUE_BUSY:
854 list_add(&rq->queuelist, &rq_list);
855 __blk_mq_requeue_request(rq);
858 pr_err("blk-mq: bad return on queue: %d\n", ret);
859 case BLK_MQ_RQ_QUEUE_ERROR:
861 blk_mq_end_request(rq, rq->errors);
865 if (ret == BLK_MQ_RQ_QUEUE_BUSY)
869 * We've done the first request. If we have more than 1
870 * left in the list, set dptr to defer issue.
872 if (!dptr && rq_list.next != rq_list.prev)
877 hctx->dispatched[0]++;
878 else if (queued < (1 << (BLK_MQ_MAX_DISPATCH_ORDER - 1)))
879 hctx->dispatched[ilog2(queued) + 1]++;
882 * Any items that need requeuing? Stuff them into hctx->dispatch,
883 * that is where we will continue on next queue run.
885 if (!list_empty(&rq_list)) {
886 spin_lock(&hctx->lock);
887 list_splice(&rq_list, &hctx->dispatch);
888 spin_unlock(&hctx->lock);
890 * the queue is expected stopped with BLK_MQ_RQ_QUEUE_BUSY, but
891 * it's possible the queue is stopped and restarted again
892 * before this. Queue restart will dispatch requests. And since
893 * requests in rq_list aren't added into hctx->dispatch yet,
894 * the requests in rq_list might get lost.
896 * blk_mq_run_hw_queue() already checks the STOPPED bit
898 blk_mq_run_hw_queue(hctx, true);
903 * It'd be great if the workqueue API had a way to pass
904 * in a mask and had some smarts for more clever placement.
905 * For now we just round-robin here, switching for every
906 * BLK_MQ_CPU_WORK_BATCH queued items.
908 static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx)
910 if (hctx->queue->nr_hw_queues == 1)
911 return WORK_CPU_UNBOUND;
913 if (--hctx->next_cpu_batch <= 0) {
914 int cpu = hctx->next_cpu, next_cpu;
916 next_cpu = cpumask_next(hctx->next_cpu, hctx->cpumask);
917 if (next_cpu >= nr_cpu_ids)
918 next_cpu = cpumask_first(hctx->cpumask);
920 hctx->next_cpu = next_cpu;
921 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
926 return hctx->next_cpu;
929 void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
931 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state) ||
932 !blk_mq_hw_queue_mapped(hctx)))
937 if (cpumask_test_cpu(cpu, hctx->cpumask)) {
938 __blk_mq_run_hw_queue(hctx);
946 kblockd_schedule_work_on(blk_mq_hctx_next_cpu(hctx), &hctx->run_work);
949 void blk_mq_run_hw_queues(struct request_queue *q, bool async)
951 struct blk_mq_hw_ctx *hctx;
954 queue_for_each_hw_ctx(q, hctx, i) {
955 if ((!blk_mq_hctx_has_pending(hctx) &&
956 list_empty_careful(&hctx->dispatch)) ||
957 test_bit(BLK_MQ_S_STOPPED, &hctx->state))
960 blk_mq_run_hw_queue(hctx, async);
963 EXPORT_SYMBOL(blk_mq_run_hw_queues);
965 void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
967 cancel_work(&hctx->run_work);
968 cancel_delayed_work(&hctx->delay_work);
969 set_bit(BLK_MQ_S_STOPPED, &hctx->state);
971 EXPORT_SYMBOL(blk_mq_stop_hw_queue);
973 void blk_mq_stop_hw_queues(struct request_queue *q)
975 struct blk_mq_hw_ctx *hctx;
978 queue_for_each_hw_ctx(q, hctx, i)
979 blk_mq_stop_hw_queue(hctx);
981 EXPORT_SYMBOL(blk_mq_stop_hw_queues);
983 void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
985 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
987 blk_mq_run_hw_queue(hctx, false);
989 EXPORT_SYMBOL(blk_mq_start_hw_queue);
991 void blk_mq_start_hw_queues(struct request_queue *q)
993 struct blk_mq_hw_ctx *hctx;
996 queue_for_each_hw_ctx(q, hctx, i)
997 blk_mq_start_hw_queue(hctx);
999 EXPORT_SYMBOL(blk_mq_start_hw_queues);
1001 void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async)
1003 struct blk_mq_hw_ctx *hctx;
1006 queue_for_each_hw_ctx(q, hctx, i) {
1007 if (!test_bit(BLK_MQ_S_STOPPED, &hctx->state))
1010 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
1011 blk_mq_run_hw_queue(hctx, async);
1014 EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
1016 static void blk_mq_run_work_fn(struct work_struct *work)
1018 struct blk_mq_hw_ctx *hctx;
1020 hctx = container_of(work, struct blk_mq_hw_ctx, run_work);
1022 __blk_mq_run_hw_queue(hctx);
1025 static void blk_mq_delay_work_fn(struct work_struct *work)
1027 struct blk_mq_hw_ctx *hctx;
1029 hctx = container_of(work, struct blk_mq_hw_ctx, delay_work.work);
1031 if (test_and_clear_bit(BLK_MQ_S_STOPPED, &hctx->state))
1032 __blk_mq_run_hw_queue(hctx);
1035 void blk_mq_delay_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
1037 if (unlikely(!blk_mq_hw_queue_mapped(hctx)))
1040 kblockd_schedule_delayed_work_on(blk_mq_hctx_next_cpu(hctx),
1041 &hctx->delay_work, msecs_to_jiffies(msecs));
1043 EXPORT_SYMBOL(blk_mq_delay_queue);
1045 static inline void __blk_mq_insert_req_list(struct blk_mq_hw_ctx *hctx,
1049 struct blk_mq_ctx *ctx = rq->mq_ctx;
1051 trace_block_rq_insert(hctx->queue, rq);
1054 list_add(&rq->queuelist, &ctx->rq_list);
1056 list_add_tail(&rq->queuelist, &ctx->rq_list);
1059 static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx,
1060 struct request *rq, bool at_head)
1062 struct blk_mq_ctx *ctx = rq->mq_ctx;
1064 __blk_mq_insert_req_list(hctx, rq, at_head);
1065 blk_mq_hctx_mark_pending(hctx, ctx);
1068 void blk_mq_insert_request(struct request *rq, bool at_head, bool run_queue,
1071 struct blk_mq_ctx *ctx = rq->mq_ctx;
1072 struct request_queue *q = rq->q;
1073 struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(q, ctx->cpu);
1075 spin_lock(&ctx->lock);
1076 __blk_mq_insert_request(hctx, rq, at_head);
1077 spin_unlock(&ctx->lock);
1080 blk_mq_run_hw_queue(hctx, async);
1083 static void blk_mq_insert_requests(struct request_queue *q,
1084 struct blk_mq_ctx *ctx,
1085 struct list_head *list,
1090 struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(q, ctx->cpu);
1092 trace_block_unplug(q, depth, !from_schedule);
1095 * preemption doesn't flush plug list, so it's possible ctx->cpu is
1098 spin_lock(&ctx->lock);
1099 while (!list_empty(list)) {
1102 rq = list_first_entry(list, struct request, queuelist);
1103 BUG_ON(rq->mq_ctx != ctx);
1104 list_del_init(&rq->queuelist);
1105 __blk_mq_insert_req_list(hctx, rq, false);
1107 blk_mq_hctx_mark_pending(hctx, ctx);
1108 spin_unlock(&ctx->lock);
1110 blk_mq_run_hw_queue(hctx, from_schedule);
1113 static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b)
1115 struct request *rqa = container_of(a, struct request, queuelist);
1116 struct request *rqb = container_of(b, struct request, queuelist);
1118 return !(rqa->mq_ctx < rqb->mq_ctx ||
1119 (rqa->mq_ctx == rqb->mq_ctx &&
1120 blk_rq_pos(rqa) < blk_rq_pos(rqb)));
1123 void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
1125 struct blk_mq_ctx *this_ctx;
1126 struct request_queue *this_q;
1129 LIST_HEAD(ctx_list);
1132 list_splice_init(&plug->mq_list, &list);
1134 list_sort(NULL, &list, plug_ctx_cmp);
1140 while (!list_empty(&list)) {
1141 rq = list_entry_rq(list.next);
1142 list_del_init(&rq->queuelist);
1144 if (rq->mq_ctx != this_ctx) {
1146 blk_mq_insert_requests(this_q, this_ctx,
1151 this_ctx = rq->mq_ctx;
1157 list_add_tail(&rq->queuelist, &ctx_list);
1161 * If 'this_ctx' is set, we know we have entries to complete
1162 * on 'ctx_list'. Do those.
1165 blk_mq_insert_requests(this_q, this_ctx, &ctx_list, depth,
1170 static void blk_mq_bio_to_request(struct request *rq, struct bio *bio)
1172 init_request_from_bio(rq, bio);
1174 blk_account_io_start(rq, 1);
1177 static inline bool hctx_allow_merges(struct blk_mq_hw_ctx *hctx)
1179 return (hctx->flags & BLK_MQ_F_SHOULD_MERGE) &&
1180 !blk_queue_nomerges(hctx->queue);
1183 static inline bool blk_mq_merge_queue_io(struct blk_mq_hw_ctx *hctx,
1184 struct blk_mq_ctx *ctx,
1185 struct request *rq, struct bio *bio)
1187 if (!hctx_allow_merges(hctx) || !bio_mergeable(bio)) {
1188 blk_mq_bio_to_request(rq, bio);
1189 spin_lock(&ctx->lock);
1191 __blk_mq_insert_request(hctx, rq, false);
1192 spin_unlock(&ctx->lock);
1195 struct request_queue *q = hctx->queue;
1197 spin_lock(&ctx->lock);
1198 if (!blk_mq_attempt_merge(q, ctx, bio)) {
1199 blk_mq_bio_to_request(rq, bio);
1203 spin_unlock(&ctx->lock);
1204 __blk_mq_free_request(hctx, ctx, rq);
1209 struct blk_map_ctx {
1210 struct blk_mq_hw_ctx *hctx;
1211 struct blk_mq_ctx *ctx;
1214 static struct request *blk_mq_map_request(struct request_queue *q,
1216 struct blk_map_ctx *data)
1218 struct blk_mq_hw_ctx *hctx;
1219 struct blk_mq_ctx *ctx;
1221 int op = bio_data_dir(bio);
1223 struct blk_mq_alloc_data alloc_data;
1225 blk_queue_enter_live(q);
1226 ctx = blk_mq_get_ctx(q);
1227 hctx = blk_mq_map_queue(q, ctx->cpu);
1229 if (rw_is_sync(bio_op(bio), bio->bi_opf))
1230 op_flags |= REQ_SYNC;
1232 trace_block_getrq(q, bio, op);
1233 blk_mq_set_alloc_data(&alloc_data, q, BLK_MQ_REQ_NOWAIT, ctx, hctx);
1234 rq = __blk_mq_alloc_request(&alloc_data, op, op_flags);
1235 if (unlikely(!rq)) {
1236 __blk_mq_run_hw_queue(hctx);
1237 blk_mq_put_ctx(ctx);
1238 trace_block_sleeprq(q, bio, op);
1240 ctx = blk_mq_get_ctx(q);
1241 hctx = blk_mq_map_queue(q, ctx->cpu);
1242 blk_mq_set_alloc_data(&alloc_data, q, 0, ctx, hctx);
1243 rq = __blk_mq_alloc_request(&alloc_data, op, op_flags);
1244 ctx = alloc_data.ctx;
1245 hctx = alloc_data.hctx;
1254 static int blk_mq_direct_issue_request(struct request *rq, blk_qc_t *cookie)
1257 struct request_queue *q = rq->q;
1258 struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(q, rq->mq_ctx->cpu);
1259 struct blk_mq_queue_data bd = {
1264 blk_qc_t new_cookie = blk_tag_to_qc_t(rq->tag, hctx->queue_num);
1267 * For OK queue, we are done. For error, kill it. Any other
1268 * error (busy), just add it to our list as we previously
1271 ret = q->mq_ops->queue_rq(hctx, &bd);
1272 if (ret == BLK_MQ_RQ_QUEUE_OK) {
1273 *cookie = new_cookie;
1277 __blk_mq_requeue_request(rq);
1279 if (ret == BLK_MQ_RQ_QUEUE_ERROR) {
1280 *cookie = BLK_QC_T_NONE;
1282 blk_mq_end_request(rq, rq->errors);
1290 * Multiple hardware queue variant. This will not use per-process plugs,
1291 * but will attempt to bypass the hctx queueing if we can go straight to
1292 * hardware for SYNC IO.
1294 static blk_qc_t blk_mq_make_request(struct request_queue *q, struct bio *bio)
1296 const int is_sync = rw_is_sync(bio_op(bio), bio->bi_opf);
1297 const int is_flush_fua = bio->bi_opf & (REQ_PREFLUSH | REQ_FUA);
1298 struct blk_map_ctx data;
1300 unsigned int request_count = 0;
1301 struct blk_plug *plug;
1302 struct request *same_queue_rq = NULL;
1305 blk_queue_bounce(q, &bio);
1307 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
1309 return BLK_QC_T_NONE;
1312 blk_queue_split(q, &bio, q->bio_split);
1314 if (!is_flush_fua && !blk_queue_nomerges(q) &&
1315 blk_attempt_plug_merge(q, bio, &request_count, &same_queue_rq))
1316 return BLK_QC_T_NONE;
1318 rq = blk_mq_map_request(q, bio, &data);
1320 return BLK_QC_T_NONE;
1322 cookie = blk_tag_to_qc_t(rq->tag, data.hctx->queue_num);
1324 if (unlikely(is_flush_fua)) {
1325 blk_mq_bio_to_request(rq, bio);
1326 blk_insert_flush(rq);
1330 plug = current->plug;
1332 * If the driver supports defer issued based on 'last', then
1333 * queue it up like normal since we can potentially save some
1336 if (((plug && !blk_queue_nomerges(q)) || is_sync) &&
1337 !(data.hctx->flags & BLK_MQ_F_DEFER_ISSUE)) {
1338 struct request *old_rq = NULL;
1340 blk_mq_bio_to_request(rq, bio);
1343 * We do limited pluging. If the bio can be merged, do that.
1344 * Otherwise the existing request in the plug list will be
1345 * issued. So the plug list will have one request at most
1349 * The plug list might get flushed before this. If that
1350 * happens, same_queue_rq is invalid and plug list is
1353 if (same_queue_rq && !list_empty(&plug->mq_list)) {
1354 old_rq = same_queue_rq;
1355 list_del_init(&old_rq->queuelist);
1357 list_add_tail(&rq->queuelist, &plug->mq_list);
1358 } else /* is_sync */
1360 blk_mq_put_ctx(data.ctx);
1363 if (!blk_mq_direct_issue_request(old_rq, &cookie))
1365 blk_mq_insert_request(old_rq, false, true, true);
1369 if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) {
1371 * For a SYNC request, send it to the hardware immediately. For
1372 * an ASYNC request, just ensure that we run it later on. The
1373 * latter allows for merging opportunities and more efficient
1377 blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua);
1379 blk_mq_put_ctx(data.ctx);
1385 * Single hardware queue variant. This will attempt to use any per-process
1386 * plug for merging and IO deferral.
1388 static blk_qc_t blk_sq_make_request(struct request_queue *q, struct bio *bio)
1390 const int is_sync = rw_is_sync(bio_op(bio), bio->bi_opf);
1391 const int is_flush_fua = bio->bi_opf & (REQ_PREFLUSH | REQ_FUA);
1392 struct blk_plug *plug;
1393 unsigned int request_count = 0;
1394 struct blk_map_ctx data;
1398 blk_queue_bounce(q, &bio);
1400 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
1402 return BLK_QC_T_NONE;
1405 blk_queue_split(q, &bio, q->bio_split);
1407 if (!is_flush_fua && !blk_queue_nomerges(q)) {
1408 if (blk_attempt_plug_merge(q, bio, &request_count, NULL))
1409 return BLK_QC_T_NONE;
1411 request_count = blk_plug_queued_count(q);
1413 rq = blk_mq_map_request(q, bio, &data);
1415 return BLK_QC_T_NONE;
1417 cookie = blk_tag_to_qc_t(rq->tag, data.hctx->queue_num);
1419 if (unlikely(is_flush_fua)) {
1420 blk_mq_bio_to_request(rq, bio);
1421 blk_insert_flush(rq);
1426 * A task plug currently exists. Since this is completely lockless,
1427 * utilize that to temporarily store requests until the task is
1428 * either done or scheduled away.
1430 plug = current->plug;
1432 blk_mq_bio_to_request(rq, bio);
1434 trace_block_plug(q);
1436 blk_mq_put_ctx(data.ctx);
1438 if (request_count >= BLK_MAX_REQUEST_COUNT) {
1439 blk_flush_plug_list(plug, false);
1440 trace_block_plug(q);
1443 list_add_tail(&rq->queuelist, &plug->mq_list);
1447 if (!blk_mq_merge_queue_io(data.hctx, data.ctx, rq, bio)) {
1449 * For a SYNC request, send it to the hardware immediately. For
1450 * an ASYNC request, just ensure that we run it later on. The
1451 * latter allows for merging opportunities and more efficient
1455 blk_mq_run_hw_queue(data.hctx, !is_sync || is_flush_fua);
1458 blk_mq_put_ctx(data.ctx);
1462 static void blk_mq_free_rq_map(struct blk_mq_tag_set *set,
1463 struct blk_mq_tags *tags, unsigned int hctx_idx)
1467 if (tags->rqs && set->ops->exit_request) {
1470 for (i = 0; i < tags->nr_tags; i++) {
1473 set->ops->exit_request(set->driver_data, tags->rqs[i],
1475 tags->rqs[i] = NULL;
1479 while (!list_empty(&tags->page_list)) {
1480 page = list_first_entry(&tags->page_list, struct page, lru);
1481 list_del_init(&page->lru);
1483 * Remove kmemleak object previously allocated in
1484 * blk_mq_init_rq_map().
1486 kmemleak_free(page_address(page));
1487 __free_pages(page, page->private);
1492 blk_mq_free_tags(tags);
1495 static size_t order_to_size(unsigned int order)
1497 return (size_t)PAGE_SIZE << order;
1500 static struct blk_mq_tags *blk_mq_init_rq_map(struct blk_mq_tag_set *set,
1501 unsigned int hctx_idx)
1503 struct blk_mq_tags *tags;
1504 unsigned int i, j, entries_per_page, max_order = 4;
1505 size_t rq_size, left;
1507 tags = blk_mq_init_tags(set->queue_depth, set->reserved_tags,
1509 BLK_MQ_FLAG_TO_ALLOC_POLICY(set->flags));
1513 INIT_LIST_HEAD(&tags->page_list);
1515 tags->rqs = kzalloc_node(set->queue_depth * sizeof(struct request *),
1516 GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY,
1519 blk_mq_free_tags(tags);
1524 * rq_size is the size of the request plus driver payload, rounded
1525 * to the cacheline size
1527 rq_size = round_up(sizeof(struct request) + set->cmd_size,
1529 left = rq_size * set->queue_depth;
1531 for (i = 0; i < set->queue_depth; ) {
1532 int this_order = max_order;
1537 while (this_order && left < order_to_size(this_order - 1))
1541 page = alloc_pages_node(set->numa_node,
1542 GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO,
1548 if (order_to_size(this_order) < rq_size)
1555 page->private = this_order;
1556 list_add_tail(&page->lru, &tags->page_list);
1558 p = page_address(page);
1560 * Allow kmemleak to scan these pages as they contain pointers
1561 * to additional allocations like via ops->init_request().
1563 kmemleak_alloc(p, order_to_size(this_order), 1, GFP_KERNEL);
1564 entries_per_page = order_to_size(this_order) / rq_size;
1565 to_do = min(entries_per_page, set->queue_depth - i);
1566 left -= to_do * rq_size;
1567 for (j = 0; j < to_do; j++) {
1569 if (set->ops->init_request) {
1570 if (set->ops->init_request(set->driver_data,
1571 tags->rqs[i], hctx_idx, i,
1573 tags->rqs[i] = NULL;
1585 blk_mq_free_rq_map(set, tags, hctx_idx);
1589 static void blk_mq_free_bitmap(struct blk_mq_ctxmap *bitmap)
1594 static int blk_mq_alloc_bitmap(struct blk_mq_ctxmap *bitmap, int node)
1596 unsigned int bpw = 8, total, num_maps, i;
1598 bitmap->bits_per_word = bpw;
1600 num_maps = ALIGN(nr_cpu_ids, bpw) / bpw;
1601 bitmap->map = kzalloc_node(num_maps * sizeof(struct blk_align_bitmap),
1607 for (i = 0; i < num_maps; i++) {
1608 bitmap->map[i].depth = min(total, bitmap->bits_per_word);
1609 total -= bitmap->map[i].depth;
1616 * 'cpu' is going away. splice any existing rq_list entries from this
1617 * software queue to the hw queue dispatch list, and ensure that it
1620 static int blk_mq_hctx_cpu_offline(struct blk_mq_hw_ctx *hctx, int cpu)
1622 struct blk_mq_ctx *ctx;
1625 ctx = __blk_mq_get_ctx(hctx->queue, cpu);
1627 spin_lock(&ctx->lock);
1628 if (!list_empty(&ctx->rq_list)) {
1629 list_splice_init(&ctx->rq_list, &tmp);
1630 blk_mq_hctx_clear_pending(hctx, ctx);
1632 spin_unlock(&ctx->lock);
1634 if (list_empty(&tmp))
1637 spin_lock(&hctx->lock);
1638 list_splice_tail_init(&tmp, &hctx->dispatch);
1639 spin_unlock(&hctx->lock);
1641 blk_mq_run_hw_queue(hctx, true);
1645 static int blk_mq_hctx_notify(void *data, unsigned long action,
1648 struct blk_mq_hw_ctx *hctx = data;
1650 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN)
1651 return blk_mq_hctx_cpu_offline(hctx, cpu);
1654 * In case of CPU online, tags may be reallocated
1655 * in blk_mq_map_swqueue() after mapping is updated.
1661 /* hctx->ctxs will be freed in queue's release handler */
1662 static void blk_mq_exit_hctx(struct request_queue *q,
1663 struct blk_mq_tag_set *set,
1664 struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
1666 unsigned flush_start_tag = set->queue_depth;
1668 blk_mq_tag_idle(hctx);
1670 if (set->ops->exit_request)
1671 set->ops->exit_request(set->driver_data,
1672 hctx->fq->flush_rq, hctx_idx,
1673 flush_start_tag + hctx_idx);
1675 if (set->ops->exit_hctx)
1676 set->ops->exit_hctx(hctx, hctx_idx);
1678 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
1679 blk_free_flush_queue(hctx->fq);
1680 blk_mq_free_bitmap(&hctx->ctx_map);
1683 static void blk_mq_exit_hw_queues(struct request_queue *q,
1684 struct blk_mq_tag_set *set, int nr_queue)
1686 struct blk_mq_hw_ctx *hctx;
1689 queue_for_each_hw_ctx(q, hctx, i) {
1692 blk_mq_exit_hctx(q, set, hctx, i);
1696 static void blk_mq_free_hw_queues(struct request_queue *q,
1697 struct blk_mq_tag_set *set)
1699 struct blk_mq_hw_ctx *hctx;
1702 queue_for_each_hw_ctx(q, hctx, i)
1703 free_cpumask_var(hctx->cpumask);
1706 static int blk_mq_init_hctx(struct request_queue *q,
1707 struct blk_mq_tag_set *set,
1708 struct blk_mq_hw_ctx *hctx, unsigned hctx_idx)
1711 unsigned flush_start_tag = set->queue_depth;
1713 node = hctx->numa_node;
1714 if (node == NUMA_NO_NODE)
1715 node = hctx->numa_node = set->numa_node;
1717 INIT_WORK(&hctx->run_work, blk_mq_run_work_fn);
1718 INIT_DELAYED_WORK(&hctx->delay_work, blk_mq_delay_work_fn);
1719 spin_lock_init(&hctx->lock);
1720 INIT_LIST_HEAD(&hctx->dispatch);
1722 hctx->queue_num = hctx_idx;
1723 hctx->flags = set->flags & ~BLK_MQ_F_TAG_SHARED;
1725 blk_mq_init_cpu_notifier(&hctx->cpu_notifier,
1726 blk_mq_hctx_notify, hctx);
1727 blk_mq_register_cpu_notifier(&hctx->cpu_notifier);
1729 hctx->tags = set->tags[hctx_idx];
1732 * Allocate space for all possible cpus to avoid allocation at
1735 hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *),
1738 goto unregister_cpu_notifier;
1740 if (blk_mq_alloc_bitmap(&hctx->ctx_map, node))
1745 if (set->ops->init_hctx &&
1746 set->ops->init_hctx(hctx, set->driver_data, hctx_idx))
1749 hctx->fq = blk_alloc_flush_queue(q, hctx->numa_node, set->cmd_size);
1753 if (set->ops->init_request &&
1754 set->ops->init_request(set->driver_data,
1755 hctx->fq->flush_rq, hctx_idx,
1756 flush_start_tag + hctx_idx, node))
1764 if (set->ops->exit_hctx)
1765 set->ops->exit_hctx(hctx, hctx_idx);
1767 blk_mq_free_bitmap(&hctx->ctx_map);
1770 unregister_cpu_notifier:
1771 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
1776 static void blk_mq_init_cpu_queues(struct request_queue *q,
1777 unsigned int nr_hw_queues)
1781 for_each_possible_cpu(i) {
1782 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
1783 struct blk_mq_hw_ctx *hctx;
1785 memset(__ctx, 0, sizeof(*__ctx));
1787 spin_lock_init(&__ctx->lock);
1788 INIT_LIST_HEAD(&__ctx->rq_list);
1791 /* If the cpu isn't online, the cpu is mapped to first hctx */
1795 hctx = blk_mq_map_queue(q, i);
1798 * Set local node, IFF we have more than one hw queue. If
1799 * not, we remain on the home node of the device
1801 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
1802 hctx->numa_node = local_memory_node(cpu_to_node(i));
1806 static void blk_mq_map_swqueue(struct request_queue *q,
1807 const struct cpumask *online_mask)
1810 struct blk_mq_hw_ctx *hctx;
1811 struct blk_mq_ctx *ctx;
1812 struct blk_mq_tag_set *set = q->tag_set;
1815 * Avoid others reading imcomplete hctx->cpumask through sysfs
1817 mutex_lock(&q->sysfs_lock);
1819 queue_for_each_hw_ctx(q, hctx, i) {
1820 cpumask_clear(hctx->cpumask);
1825 * Map software to hardware queues
1827 for_each_possible_cpu(i) {
1828 /* If the cpu isn't online, the cpu is mapped to first hctx */
1829 if (!cpumask_test_cpu(i, online_mask))
1832 ctx = per_cpu_ptr(q->queue_ctx, i);
1833 hctx = blk_mq_map_queue(q, i);
1835 cpumask_set_cpu(i, hctx->cpumask);
1836 ctx->index_hw = hctx->nr_ctx;
1837 hctx->ctxs[hctx->nr_ctx++] = ctx;
1840 mutex_unlock(&q->sysfs_lock);
1842 queue_for_each_hw_ctx(q, hctx, i) {
1843 struct blk_mq_ctxmap *map = &hctx->ctx_map;
1846 * If no software queues are mapped to this hardware queue,
1847 * disable it and free the request entries.
1849 if (!hctx->nr_ctx) {
1851 blk_mq_free_rq_map(set, set->tags[i], i);
1852 set->tags[i] = NULL;
1858 /* unmapped hw queue can be remapped after CPU topo changed */
1860 set->tags[i] = blk_mq_init_rq_map(set, i);
1861 hctx->tags = set->tags[i];
1862 WARN_ON(!hctx->tags);
1865 * Set the map size to the number of mapped software queues.
1866 * This is more accurate and more efficient than looping
1867 * over all possibly mapped software queues.
1869 map->size = DIV_ROUND_UP(hctx->nr_ctx, map->bits_per_word);
1872 * Initialize batch roundrobin counts
1874 hctx->next_cpu = cpumask_first(hctx->cpumask);
1875 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
1879 static void queue_set_hctx_shared(struct request_queue *q, bool shared)
1881 struct blk_mq_hw_ctx *hctx;
1884 queue_for_each_hw_ctx(q, hctx, i) {
1886 hctx->flags |= BLK_MQ_F_TAG_SHARED;
1888 hctx->flags &= ~BLK_MQ_F_TAG_SHARED;
1892 static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set *set, bool shared)
1894 struct request_queue *q;
1896 list_for_each_entry(q, &set->tag_list, tag_set_list) {
1897 blk_mq_freeze_queue(q);
1898 queue_set_hctx_shared(q, shared);
1899 blk_mq_unfreeze_queue(q);
1903 static void blk_mq_del_queue_tag_set(struct request_queue *q)
1905 struct blk_mq_tag_set *set = q->tag_set;
1907 mutex_lock(&set->tag_list_lock);
1908 list_del_init(&q->tag_set_list);
1909 if (list_is_singular(&set->tag_list)) {
1910 /* just transitioned to unshared */
1911 set->flags &= ~BLK_MQ_F_TAG_SHARED;
1912 /* update existing queue */
1913 blk_mq_update_tag_set_depth(set, false);
1915 mutex_unlock(&set->tag_list_lock);
1918 static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set,
1919 struct request_queue *q)
1923 mutex_lock(&set->tag_list_lock);
1925 /* Check to see if we're transitioning to shared (from 1 to 2 queues). */
1926 if (!list_empty(&set->tag_list) && !(set->flags & BLK_MQ_F_TAG_SHARED)) {
1927 set->flags |= BLK_MQ_F_TAG_SHARED;
1928 /* update existing queue */
1929 blk_mq_update_tag_set_depth(set, true);
1931 if (set->flags & BLK_MQ_F_TAG_SHARED)
1932 queue_set_hctx_shared(q, true);
1933 list_add_tail(&q->tag_set_list, &set->tag_list);
1935 mutex_unlock(&set->tag_list_lock);
1939 * It is the actual release handler for mq, but we do it from
1940 * request queue's release handler for avoiding use-after-free
1941 * and headache because q->mq_kobj shouldn't have been introduced,
1942 * but we can't group ctx/kctx kobj without it.
1944 void blk_mq_release(struct request_queue *q)
1946 struct blk_mq_hw_ctx *hctx;
1949 /* hctx kobj stays in hctx */
1950 queue_for_each_hw_ctx(q, hctx, i) {
1959 kfree(q->queue_hw_ctx);
1961 /* ctx kobj stays in queue_ctx */
1962 free_percpu(q->queue_ctx);
1965 struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
1967 struct request_queue *uninit_q, *q;
1969 uninit_q = blk_alloc_queue_node(GFP_KERNEL, set->numa_node);
1971 return ERR_PTR(-ENOMEM);
1973 q = blk_mq_init_allocated_queue(set, uninit_q);
1975 blk_cleanup_queue(uninit_q);
1979 EXPORT_SYMBOL(blk_mq_init_queue);
1981 static void blk_mq_realloc_hw_ctxs(struct blk_mq_tag_set *set,
1982 struct request_queue *q)
1985 struct blk_mq_hw_ctx **hctxs = q->queue_hw_ctx;
1987 blk_mq_sysfs_unregister(q);
1988 for (i = 0; i < set->nr_hw_queues; i++) {
1994 node = blk_mq_hw_queue_to_node(q->mq_map, i);
1995 hctxs[i] = kzalloc_node(sizeof(struct blk_mq_hw_ctx),
2000 if (!zalloc_cpumask_var_node(&hctxs[i]->cpumask, GFP_KERNEL,
2007 atomic_set(&hctxs[i]->nr_active, 0);
2008 hctxs[i]->numa_node = node;
2009 hctxs[i]->queue_num = i;
2011 if (blk_mq_init_hctx(q, set, hctxs[i], i)) {
2012 free_cpumask_var(hctxs[i]->cpumask);
2017 blk_mq_hctx_kobj_init(hctxs[i]);
2019 for (j = i; j < q->nr_hw_queues; j++) {
2020 struct blk_mq_hw_ctx *hctx = hctxs[j];
2024 blk_mq_free_rq_map(set, hctx->tags, j);
2025 set->tags[j] = NULL;
2027 blk_mq_exit_hctx(q, set, hctx, j);
2028 free_cpumask_var(hctx->cpumask);
2029 kobject_put(&hctx->kobj);
2036 q->nr_hw_queues = i;
2037 blk_mq_sysfs_register(q);
2040 struct request_queue *blk_mq_init_allocated_queue(struct blk_mq_tag_set *set,
2041 struct request_queue *q)
2043 /* mark the queue as mq asap */
2044 q->mq_ops = set->ops;
2046 q->queue_ctx = alloc_percpu(struct blk_mq_ctx);
2050 q->queue_hw_ctx = kzalloc_node(nr_cpu_ids * sizeof(*(q->queue_hw_ctx)),
2051 GFP_KERNEL, set->numa_node);
2052 if (!q->queue_hw_ctx)
2055 q->mq_map = set->mq_map;
2057 blk_mq_realloc_hw_ctxs(set, q);
2058 if (!q->nr_hw_queues)
2061 INIT_WORK(&q->timeout_work, blk_mq_timeout_work);
2062 blk_queue_rq_timeout(q, set->timeout ? set->timeout : 30 * HZ);
2064 q->nr_queues = nr_cpu_ids;
2066 q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
2068 if (!(set->flags & BLK_MQ_F_SG_MERGE))
2069 q->queue_flags |= 1 << QUEUE_FLAG_NO_SG_MERGE;
2071 q->sg_reserved_size = INT_MAX;
2073 INIT_DELAYED_WORK(&q->requeue_work, blk_mq_requeue_work);
2074 INIT_LIST_HEAD(&q->requeue_list);
2075 spin_lock_init(&q->requeue_lock);
2077 if (q->nr_hw_queues > 1)
2078 blk_queue_make_request(q, blk_mq_make_request);
2080 blk_queue_make_request(q, blk_sq_make_request);
2083 * Do this after blk_queue_make_request() overrides it...
2085 q->nr_requests = set->queue_depth;
2087 if (set->ops->complete)
2088 blk_queue_softirq_done(q, set->ops->complete);
2090 blk_mq_init_cpu_queues(q, set->nr_hw_queues);
2093 mutex_lock(&all_q_mutex);
2095 list_add_tail(&q->all_q_node, &all_q_list);
2096 blk_mq_add_queue_tag_set(set, q);
2097 blk_mq_map_swqueue(q, cpu_online_mask);
2099 mutex_unlock(&all_q_mutex);
2105 kfree(q->queue_hw_ctx);
2107 free_percpu(q->queue_ctx);
2110 return ERR_PTR(-ENOMEM);
2112 EXPORT_SYMBOL(blk_mq_init_allocated_queue);
2114 void blk_mq_free_queue(struct request_queue *q)
2116 struct blk_mq_tag_set *set = q->tag_set;
2118 mutex_lock(&all_q_mutex);
2119 list_del_init(&q->all_q_node);
2120 mutex_unlock(&all_q_mutex);
2122 blk_mq_del_queue_tag_set(q);
2124 blk_mq_exit_hw_queues(q, set, set->nr_hw_queues);
2125 blk_mq_free_hw_queues(q, set);
2128 /* Basically redo blk_mq_init_queue with queue frozen */
2129 static void blk_mq_queue_reinit(struct request_queue *q,
2130 const struct cpumask *online_mask)
2132 WARN_ON_ONCE(!atomic_read(&q->mq_freeze_depth));
2134 blk_mq_sysfs_unregister(q);
2137 * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
2138 * we should change hctx numa_node according to new topology (this
2139 * involves free and re-allocate memory, worthy doing?)
2142 blk_mq_map_swqueue(q, online_mask);
2144 blk_mq_sysfs_register(q);
2147 static int blk_mq_queue_reinit_notify(struct notifier_block *nb,
2148 unsigned long action, void *hcpu)
2150 struct request_queue *q;
2151 int cpu = (unsigned long)hcpu;
2153 * New online cpumask which is going to be set in this hotplug event.
2154 * Declare this cpumasks as global as cpu-hotplug operation is invoked
2155 * one-by-one and dynamically allocating this could result in a failure.
2157 static struct cpumask online_new;
2160 * Before hotadded cpu starts handling requests, new mappings must
2161 * be established. Otherwise, these requests in hw queue might
2162 * never be dispatched.
2164 * For example, there is a single hw queue (hctx) and two CPU queues
2165 * (ctx0 for CPU0, and ctx1 for CPU1).
2167 * Now CPU1 is just onlined and a request is inserted into
2168 * ctx1->rq_list and set bit0 in pending bitmap as ctx1->index_hw is
2171 * And then while running hw queue, flush_busy_ctxs() finds bit0 is
2172 * set in pending bitmap and tries to retrieve requests in
2173 * hctx->ctxs[0]->rq_list. But htx->ctxs[0] is a pointer to ctx0,
2174 * so the request in ctx1->rq_list is ignored.
2176 switch (action & ~CPU_TASKS_FROZEN) {
2178 case CPU_UP_CANCELED:
2179 cpumask_copy(&online_new, cpu_online_mask);
2181 case CPU_UP_PREPARE:
2182 cpumask_copy(&online_new, cpu_online_mask);
2183 cpumask_set_cpu(cpu, &online_new);
2189 mutex_lock(&all_q_mutex);
2192 * We need to freeze and reinit all existing queues. Freezing
2193 * involves synchronous wait for an RCU grace period and doing it
2194 * one by one may take a long time. Start freezing all queues in
2195 * one swoop and then wait for the completions so that freezing can
2196 * take place in parallel.
2198 list_for_each_entry(q, &all_q_list, all_q_node)
2199 blk_mq_freeze_queue_start(q);
2200 list_for_each_entry(q, &all_q_list, all_q_node) {
2201 blk_mq_freeze_queue_wait(q);
2204 * timeout handler can't touch hw queue during the
2207 del_timer_sync(&q->timeout);
2210 list_for_each_entry(q, &all_q_list, all_q_node)
2211 blk_mq_queue_reinit(q, &online_new);
2213 list_for_each_entry(q, &all_q_list, all_q_node)
2214 blk_mq_unfreeze_queue(q);
2216 mutex_unlock(&all_q_mutex);
2220 static int __blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set)
2224 for (i = 0; i < set->nr_hw_queues; i++) {
2225 set->tags[i] = blk_mq_init_rq_map(set, i);
2234 blk_mq_free_rq_map(set, set->tags[i], i);
2240 * Allocate the request maps associated with this tag_set. Note that this
2241 * may reduce the depth asked for, if memory is tight. set->queue_depth
2242 * will be updated to reflect the allocated depth.
2244 static int blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set)
2249 depth = set->queue_depth;
2251 err = __blk_mq_alloc_rq_maps(set);
2255 set->queue_depth >>= 1;
2256 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) {
2260 } while (set->queue_depth);
2262 if (!set->queue_depth || err) {
2263 pr_err("blk-mq: failed to allocate request map\n");
2267 if (depth != set->queue_depth)
2268 pr_info("blk-mq: reduced tag depth (%u -> %u)\n",
2269 depth, set->queue_depth);
2275 * Alloc a tag set to be associated with one or more request queues.
2276 * May fail with EINVAL for various error conditions. May adjust the
2277 * requested depth down, if if it too large. In that case, the set
2278 * value will be stored in set->queue_depth.
2280 int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
2284 BUILD_BUG_ON(BLK_MQ_MAX_DEPTH > 1 << BLK_MQ_UNIQUE_TAG_BITS);
2286 if (!set->nr_hw_queues)
2288 if (!set->queue_depth)
2290 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN)
2293 if (!set->ops->queue_rq)
2296 if (set->queue_depth > BLK_MQ_MAX_DEPTH) {
2297 pr_info("blk-mq: reduced tag depth to %u\n",
2299 set->queue_depth = BLK_MQ_MAX_DEPTH;
2303 * If a crashdump is active, then we are potentially in a very
2304 * memory constrained environment. Limit us to 1 queue and
2305 * 64 tags to prevent using too much memory.
2307 if (is_kdump_kernel()) {
2308 set->nr_hw_queues = 1;
2309 set->queue_depth = min(64U, set->queue_depth);
2312 * There is no use for more h/w queues than cpus.
2314 if (set->nr_hw_queues > nr_cpu_ids)
2315 set->nr_hw_queues = nr_cpu_ids;
2317 set->tags = kzalloc_node(nr_cpu_ids * sizeof(struct blk_mq_tags *),
2318 GFP_KERNEL, set->numa_node);
2323 set->mq_map = kzalloc_node(sizeof(*set->mq_map) * nr_cpu_ids,
2324 GFP_KERNEL, set->numa_node);
2328 if (set->ops->map_queues)
2329 ret = set->ops->map_queues(set);
2331 ret = blk_mq_map_queues(set);
2333 goto out_free_mq_map;
2335 ret = blk_mq_alloc_rq_maps(set);
2337 goto out_free_mq_map;
2339 mutex_init(&set->tag_list_lock);
2340 INIT_LIST_HEAD(&set->tag_list);
2352 EXPORT_SYMBOL(blk_mq_alloc_tag_set);
2354 void blk_mq_free_tag_set(struct blk_mq_tag_set *set)
2358 for (i = 0; i < nr_cpu_ids; i++) {
2360 blk_mq_free_rq_map(set, set->tags[i], i);
2369 EXPORT_SYMBOL(blk_mq_free_tag_set);
2371 int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr)
2373 struct blk_mq_tag_set *set = q->tag_set;
2374 struct blk_mq_hw_ctx *hctx;
2377 if (!set || nr > set->queue_depth)
2381 queue_for_each_hw_ctx(q, hctx, i) {
2384 ret = blk_mq_tag_update_depth(hctx->tags, nr);
2390 q->nr_requests = nr;
2395 void blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set, int nr_hw_queues)
2397 struct request_queue *q;
2399 if (nr_hw_queues > nr_cpu_ids)
2400 nr_hw_queues = nr_cpu_ids;
2401 if (nr_hw_queues < 1 || nr_hw_queues == set->nr_hw_queues)
2404 list_for_each_entry(q, &set->tag_list, tag_set_list)
2405 blk_mq_freeze_queue(q);
2407 set->nr_hw_queues = nr_hw_queues;
2408 list_for_each_entry(q, &set->tag_list, tag_set_list) {
2409 blk_mq_realloc_hw_ctxs(set, q);
2411 if (q->nr_hw_queues > 1)
2412 blk_queue_make_request(q, blk_mq_make_request);
2414 blk_queue_make_request(q, blk_sq_make_request);
2416 blk_mq_queue_reinit(q, cpu_online_mask);
2419 list_for_each_entry(q, &set->tag_list, tag_set_list)
2420 blk_mq_unfreeze_queue(q);
2422 EXPORT_SYMBOL_GPL(blk_mq_update_nr_hw_queues);
2424 void blk_mq_disable_hotplug(void)
2426 mutex_lock(&all_q_mutex);
2429 void blk_mq_enable_hotplug(void)
2431 mutex_unlock(&all_q_mutex);
2434 static int __init blk_mq_init(void)
2438 hotcpu_notifier(blk_mq_queue_reinit_notify, 0);
2442 subsys_initcall(blk_mq_init);