1 #include <linux/kernel.h>
2 #include <linux/module.h>
3 #include <linux/backing-dev.h>
5 #include <linux/blkdev.h>
7 #include <linux/init.h>
8 #include <linux/slab.h>
9 #include <linux/workqueue.h>
10 #include <linux/smp.h>
11 #include <linux/llist.h>
12 #include <linux/list_sort.h>
13 #include <linux/cpu.h>
14 #include <linux/cache.h>
15 #include <linux/sched/sysctl.h>
16 #include <linux/delay.h>
18 #include <trace/events/block.h>
20 #include <linux/blk-mq.h>
23 #include "blk-mq-tag.h"
25 static DEFINE_MUTEX(all_q_mutex
);
26 static LIST_HEAD(all_q_list
);
28 static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx
*hctx
);
30 DEFINE_PER_CPU(struct llist_head
, ipi_lists
);
32 static struct blk_mq_ctx
*__blk_mq_get_ctx(struct request_queue
*q
,
35 return per_cpu_ptr(q
->queue_ctx
, cpu
);
39 * This assumes per-cpu software queueing queues. They could be per-node
40 * as well, for instance. For now this is hardcoded as-is. Note that we don't
41 * care about preemption, since we know the ctx's are persistent. This does
42 * mean that we can't rely on ctx always matching the currently running CPU.
44 static struct blk_mq_ctx
*blk_mq_get_ctx(struct request_queue
*q
)
46 return __blk_mq_get_ctx(q
, get_cpu());
49 static void blk_mq_put_ctx(struct blk_mq_ctx
*ctx
)
55 * Check if any of the ctx's have pending work in this hardware queue
57 static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx
*hctx
)
61 for (i
= 0; i
< hctx
->nr_ctx_map
; i
++)
69 * Mark this ctx as having pending work in this hardware queue
71 static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx
*hctx
,
72 struct blk_mq_ctx
*ctx
)
74 if (!test_bit(ctx
->index_hw
, hctx
->ctx_map
))
75 set_bit(ctx
->index_hw
, hctx
->ctx_map
);
78 static struct request
*blk_mq_alloc_rq(struct blk_mq_hw_ctx
*hctx
, gfp_t gfp
,
84 tag
= blk_mq_get_tag(hctx
->tags
, gfp
, reserved
);
85 if (tag
!= BLK_MQ_TAG_FAIL
) {
95 static int blk_mq_queue_enter(struct request_queue
*q
)
99 __percpu_counter_add(&q
->mq_usage_counter
, 1, 1000000);
101 /* we have problems to freeze the queue if it's initializing */
102 if (!blk_queue_bypass(q
) || !blk_queue_init_done(q
))
105 __percpu_counter_add(&q
->mq_usage_counter
, -1, 1000000);
107 spin_lock_irq(q
->queue_lock
);
108 ret
= wait_event_interruptible_lock_irq(q
->mq_freeze_wq
,
109 !blk_queue_bypass(q
), *q
->queue_lock
);
110 /* inc usage with lock hold to avoid freeze_queue runs here */
112 __percpu_counter_add(&q
->mq_usage_counter
, 1, 1000000);
113 spin_unlock_irq(q
->queue_lock
);
118 static void blk_mq_queue_exit(struct request_queue
*q
)
120 __percpu_counter_add(&q
->mq_usage_counter
, -1, 1000000);
124 * Guarantee no request is in use, so we can change any data structure of
125 * the queue afterward.
127 static void blk_mq_freeze_queue(struct request_queue
*q
)
131 spin_lock_irq(q
->queue_lock
);
132 drain
= !q
->bypass_depth
++;
133 queue_flag_set(QUEUE_FLAG_BYPASS
, q
);
134 spin_unlock_irq(q
->queue_lock
);
142 spin_lock_irq(q
->queue_lock
);
143 count
= percpu_counter_sum(&q
->mq_usage_counter
);
144 spin_unlock_irq(q
->queue_lock
);
148 blk_mq_run_queues(q
, false);
153 static void blk_mq_unfreeze_queue(struct request_queue
*q
)
157 spin_lock_irq(q
->queue_lock
);
158 if (!--q
->bypass_depth
) {
159 queue_flag_clear(QUEUE_FLAG_BYPASS
, q
);
162 WARN_ON_ONCE(q
->bypass_depth
< 0);
163 spin_unlock_irq(q
->queue_lock
);
165 wake_up_all(&q
->mq_freeze_wq
);
168 bool blk_mq_can_queue(struct blk_mq_hw_ctx
*hctx
)
170 return blk_mq_has_free_tags(hctx
->tags
);
172 EXPORT_SYMBOL(blk_mq_can_queue
);
174 static void blk_mq_rq_ctx_init(struct request_queue
*q
, struct blk_mq_ctx
*ctx
,
175 struct request
*rq
, unsigned int rw_flags
)
177 if (blk_queue_io_stat(q
))
178 rw_flags
|= REQ_IO_STAT
;
181 rq
->cmd_flags
= rw_flags
;
182 ctx
->rq_dispatched
[rw_is_sync(rw_flags
)]++;
185 static struct request
*__blk_mq_alloc_request(struct blk_mq_hw_ctx
*hctx
,
186 gfp_t gfp
, bool reserved
)
188 return blk_mq_alloc_rq(hctx
, gfp
, reserved
);
191 static struct request
*blk_mq_alloc_request_pinned(struct request_queue
*q
,
198 struct blk_mq_ctx
*ctx
= blk_mq_get_ctx(q
);
199 struct blk_mq_hw_ctx
*hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
201 rq
= __blk_mq_alloc_request(hctx
, gfp
& ~__GFP_WAIT
, reserved
);
203 blk_mq_rq_ctx_init(q
, ctx
, rq
, rw
);
205 } else if (!(gfp
& __GFP_WAIT
))
209 __blk_mq_run_hw_queue(hctx
);
210 blk_mq_wait_for_tags(hctx
->tags
);
216 struct request
*blk_mq_alloc_request(struct request_queue
*q
, int rw
,
217 gfp_t gfp
, bool reserved
)
221 if (blk_mq_queue_enter(q
))
224 rq
= blk_mq_alloc_request_pinned(q
, rw
, gfp
, reserved
);
225 blk_mq_put_ctx(rq
->mq_ctx
);
229 struct request
*blk_mq_alloc_reserved_request(struct request_queue
*q
, int rw
,
234 if (blk_mq_queue_enter(q
))
237 rq
= blk_mq_alloc_request_pinned(q
, rw
, gfp
, true);
238 blk_mq_put_ctx(rq
->mq_ctx
);
241 EXPORT_SYMBOL(blk_mq_alloc_reserved_request
);
244 * Re-init and set pdu, if we have it
246 static void blk_mq_rq_init(struct blk_mq_hw_ctx
*hctx
, struct request
*rq
)
248 blk_rq_init(hctx
->queue
, rq
);
251 rq
->special
= blk_mq_rq_to_pdu(rq
);
254 static void __blk_mq_free_request(struct blk_mq_hw_ctx
*hctx
,
255 struct blk_mq_ctx
*ctx
, struct request
*rq
)
257 const int tag
= rq
->tag
;
258 struct request_queue
*q
= rq
->q
;
260 blk_mq_rq_init(hctx
, rq
);
261 blk_mq_put_tag(hctx
->tags
, tag
);
263 blk_mq_queue_exit(q
);
266 void blk_mq_free_request(struct request
*rq
)
268 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
;
269 struct blk_mq_hw_ctx
*hctx
;
270 struct request_queue
*q
= rq
->q
;
272 ctx
->rq_completed
[rq_is_sync(rq
)]++;
274 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
275 __blk_mq_free_request(hctx
, ctx
, rq
);
278 static void blk_mq_bio_endio(struct request
*rq
, struct bio
*bio
, int error
)
281 clear_bit(BIO_UPTODATE
, &bio
->bi_flags
);
282 else if (!test_bit(BIO_UPTODATE
, &bio
->bi_flags
))
285 if (unlikely(rq
->cmd_flags
& REQ_QUIET
))
286 set_bit(BIO_QUIET
, &bio
->bi_flags
);
288 /* don't actually finish bio if it's part of flush sequence */
289 if (!(rq
->cmd_flags
& REQ_FLUSH_SEQ
))
290 bio_endio(bio
, error
);
293 void blk_mq_complete_request(struct request
*rq
, int error
)
295 struct bio
*bio
= rq
->bio
;
296 unsigned int bytes
= 0;
298 trace_block_rq_complete(rq
->q
, rq
);
301 struct bio
*next
= bio
->bi_next
;
304 bytes
+= bio
->bi_size
;
305 blk_mq_bio_endio(rq
, bio
, error
);
309 blk_account_io_completion(rq
, bytes
);
312 rq
->end_io(rq
, error
);
314 blk_mq_free_request(rq
);
316 blk_account_io_done(rq
);
319 void __blk_mq_end_io(struct request
*rq
, int error
)
321 if (!blk_mark_rq_complete(rq
))
322 blk_mq_complete_request(rq
, error
);
325 #if defined(CONFIG_SMP)
328 * Called with interrupts disabled.
330 static void ipi_end_io(void *data
)
332 struct llist_head
*list
= &per_cpu(ipi_lists
, smp_processor_id());
333 struct llist_node
*entry
, *next
;
336 entry
= llist_del_all(list
);
340 rq
= llist_entry(entry
, struct request
, ll_list
);
341 __blk_mq_end_io(rq
, rq
->errors
);
346 static int ipi_remote_cpu(struct blk_mq_ctx
*ctx
, const int cpu
,
347 struct request
*rq
, const int error
)
349 struct call_single_data
*data
= &rq
->csd
;
352 rq
->ll_list
.next
= NULL
;
355 * If the list is non-empty, an existing IPI must already
356 * be "in flight". If that is the case, we need not schedule
359 if (llist_add(&rq
->ll_list
, &per_cpu(ipi_lists
, ctx
->cpu
))) {
360 data
->func
= ipi_end_io
;
362 __smp_call_function_single(ctx
->cpu
, data
, 0);
367 #else /* CONFIG_SMP */
368 static int ipi_remote_cpu(struct blk_mq_ctx
*ctx
, const int cpu
,
369 struct request
*rq
, const int error
)
376 * End IO on this request on a multiqueue enabled driver. We'll either do
377 * it directly inline, or punt to a local IPI handler on the matching
380 void blk_mq_end_io(struct request
*rq
, int error
)
382 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
;
385 if (!ctx
->ipi_redirect
)
386 return __blk_mq_end_io(rq
, error
);
390 if (cpu
== ctx
->cpu
|| !cpu_online(ctx
->cpu
) ||
391 !ipi_remote_cpu(ctx
, cpu
, rq
, error
))
392 __blk_mq_end_io(rq
, error
);
396 EXPORT_SYMBOL(blk_mq_end_io
);
398 static void blk_mq_start_request(struct request
*rq
)
400 struct request_queue
*q
= rq
->q
;
402 trace_block_rq_issue(q
, rq
);
405 * Just mark start time and set the started bit. Due to memory
406 * ordering, we know we'll see the correct deadline as long as
407 * REQ_ATOMIC_STARTED is seen.
409 rq
->deadline
= jiffies
+ q
->rq_timeout
;
410 set_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
);
413 static void blk_mq_requeue_request(struct request
*rq
)
415 struct request_queue
*q
= rq
->q
;
417 trace_block_rq_requeue(q
, rq
);
418 clear_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
);
421 struct blk_mq_timeout_data
{
422 struct blk_mq_hw_ctx
*hctx
;
424 unsigned int *next_set
;
427 static void blk_mq_timeout_check(void *__data
, unsigned long *free_tags
)
429 struct blk_mq_timeout_data
*data
= __data
;
430 struct blk_mq_hw_ctx
*hctx
= data
->hctx
;
433 /* It may not be in flight yet (this is where
434 * the REQ_ATOMIC_STARTED flag comes in). The requests are
435 * statically allocated, so we know it's always safe to access the
436 * memory associated with a bit offset into ->rqs[].
442 tag
= find_next_zero_bit(free_tags
, hctx
->queue_depth
, tag
);
443 if (tag
>= hctx
->queue_depth
)
446 rq
= hctx
->rqs
[tag
++];
448 if (!test_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
))
451 blk_rq_check_expired(rq
, data
->next
, data
->next_set
);
455 static void blk_mq_hw_ctx_check_timeout(struct blk_mq_hw_ctx
*hctx
,
457 unsigned int *next_set
)
459 struct blk_mq_timeout_data data
= {
462 .next_set
= next_set
,
466 * Ask the tagging code to iterate busy requests, so we can
467 * check them for timeout.
469 blk_mq_tag_busy_iter(hctx
->tags
, blk_mq_timeout_check
, &data
);
472 static void blk_mq_rq_timer(unsigned long data
)
474 struct request_queue
*q
= (struct request_queue
*) data
;
475 struct blk_mq_hw_ctx
*hctx
;
476 unsigned long next
= 0;
479 queue_for_each_hw_ctx(q
, hctx
, i
)
480 blk_mq_hw_ctx_check_timeout(hctx
, &next
, &next_set
);
483 mod_timer(&q
->timeout
, round_jiffies_up(next
));
487 * Reverse check our software queue for entries that we could potentially
488 * merge with. Currently includes a hand-wavy stop count of 8, to not spend
489 * too much time checking for merges.
491 static bool blk_mq_attempt_merge(struct request_queue
*q
,
492 struct blk_mq_ctx
*ctx
, struct bio
*bio
)
497 list_for_each_entry_reverse(rq
, &ctx
->rq_list
, queuelist
) {
503 if (!blk_rq_merge_ok(rq
, bio
))
506 el_ret
= blk_try_merge(rq
, bio
);
507 if (el_ret
== ELEVATOR_BACK_MERGE
) {
508 if (bio_attempt_back_merge(q
, rq
, bio
)) {
513 } else if (el_ret
== ELEVATOR_FRONT_MERGE
) {
514 if (bio_attempt_front_merge(q
, rq
, bio
)) {
525 void blk_mq_add_timer(struct request
*rq
)
527 __blk_add_timer(rq
, NULL
);
531 * Run this hardware queue, pulling any software queues mapped to it in.
532 * Note that this function currently has various problems around ordering
533 * of IO. In particular, we'd like FIFO behaviour on handling existing
534 * items on the hctx->dispatch list. Ignore that for now.
536 static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx
*hctx
)
538 struct request_queue
*q
= hctx
->queue
;
539 struct blk_mq_ctx
*ctx
;
544 if (unlikely(test_bit(BLK_MQ_S_STOPPED
, &hctx
->flags
)))
550 * Touch any software queue that has pending entries.
552 for_each_set_bit(bit
, hctx
->ctx_map
, hctx
->nr_ctx
) {
553 clear_bit(bit
, hctx
->ctx_map
);
554 ctx
= hctx
->ctxs
[bit
];
555 BUG_ON(bit
!= ctx
->index_hw
);
557 spin_lock(&ctx
->lock
);
558 list_splice_tail_init(&ctx
->rq_list
, &rq_list
);
559 spin_unlock(&ctx
->lock
);
563 * If we have previous entries on our dispatch list, grab them
564 * and stuff them at the front for more fair dispatch.
566 if (!list_empty_careful(&hctx
->dispatch
)) {
567 spin_lock(&hctx
->lock
);
568 if (!list_empty(&hctx
->dispatch
))
569 list_splice_init(&hctx
->dispatch
, &rq_list
);
570 spin_unlock(&hctx
->lock
);
574 * Delete and return all entries from our dispatch list
579 * Now process all the entries, sending them to the driver.
581 while (!list_empty(&rq_list
)) {
584 rq
= list_first_entry(&rq_list
, struct request
, queuelist
);
585 list_del_init(&rq
->queuelist
);
586 blk_mq_start_request(rq
);
589 * Last request in the series. Flag it as such, this
590 * enables drivers to know when IO should be kicked off,
591 * if they don't do it on a per-request basis.
593 * Note: the flag isn't the only condition drivers
594 * should do kick off. If drive is busy, the last
595 * request might not have the bit set.
597 if (list_empty(&rq_list
))
598 rq
->cmd_flags
|= REQ_END
;
600 ret
= q
->mq_ops
->queue_rq(hctx
, rq
);
602 case BLK_MQ_RQ_QUEUE_OK
:
605 case BLK_MQ_RQ_QUEUE_BUSY
:
607 * FIXME: we should have a mechanism to stop the queue
608 * like blk_stop_queue, otherwise we will waste cpu
611 list_add(&rq
->queuelist
, &rq_list
);
612 blk_mq_requeue_request(rq
);
615 pr_err("blk-mq: bad return on queue: %d\n", ret
);
617 case BLK_MQ_RQ_QUEUE_ERROR
:
618 blk_mq_end_io(rq
, rq
->errors
);
622 if (ret
== BLK_MQ_RQ_QUEUE_BUSY
)
627 hctx
->dispatched
[0]++;
628 else if (queued
< (1 << (BLK_MQ_MAX_DISPATCH_ORDER
- 1)))
629 hctx
->dispatched
[ilog2(queued
) + 1]++;
632 * Any items that need requeuing? Stuff them into hctx->dispatch,
633 * that is where we will continue on next queue run.
635 if (!list_empty(&rq_list
)) {
636 spin_lock(&hctx
->lock
);
637 list_splice(&rq_list
, &hctx
->dispatch
);
638 spin_unlock(&hctx
->lock
);
642 void blk_mq_run_hw_queue(struct blk_mq_hw_ctx
*hctx
, bool async
)
644 if (unlikely(test_bit(BLK_MQ_S_STOPPED
, &hctx
->flags
)))
648 __blk_mq_run_hw_queue(hctx
);
650 struct request_queue
*q
= hctx
->queue
;
652 kblockd_schedule_delayed_work(q
, &hctx
->delayed_work
, 0);
656 void blk_mq_run_queues(struct request_queue
*q
, bool async
)
658 struct blk_mq_hw_ctx
*hctx
;
661 queue_for_each_hw_ctx(q
, hctx
, i
) {
662 if ((!blk_mq_hctx_has_pending(hctx
) &&
663 list_empty_careful(&hctx
->dispatch
)) ||
664 test_bit(BLK_MQ_S_STOPPED
, &hctx
->flags
))
667 blk_mq_run_hw_queue(hctx
, async
);
670 EXPORT_SYMBOL(blk_mq_run_queues
);
672 void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx
*hctx
)
674 cancel_delayed_work(&hctx
->delayed_work
);
675 set_bit(BLK_MQ_S_STOPPED
, &hctx
->state
);
677 EXPORT_SYMBOL(blk_mq_stop_hw_queue
);
679 void blk_mq_stop_hw_queues(struct request_queue
*q
)
681 struct blk_mq_hw_ctx
*hctx
;
684 queue_for_each_hw_ctx(q
, hctx
, i
)
685 blk_mq_stop_hw_queue(hctx
);
687 EXPORT_SYMBOL(blk_mq_stop_hw_queues
);
689 void blk_mq_start_hw_queue(struct blk_mq_hw_ctx
*hctx
)
691 clear_bit(BLK_MQ_S_STOPPED
, &hctx
->state
);
692 __blk_mq_run_hw_queue(hctx
);
694 EXPORT_SYMBOL(blk_mq_start_hw_queue
);
696 void blk_mq_start_stopped_hw_queues(struct request_queue
*q
)
698 struct blk_mq_hw_ctx
*hctx
;
701 queue_for_each_hw_ctx(q
, hctx
, i
) {
702 if (!test_bit(BLK_MQ_S_STOPPED
, &hctx
->state
))
705 clear_bit(BLK_MQ_S_STOPPED
, &hctx
->state
);
706 blk_mq_run_hw_queue(hctx
, true);
709 EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues
);
711 static void blk_mq_work_fn(struct work_struct
*work
)
713 struct blk_mq_hw_ctx
*hctx
;
715 hctx
= container_of(work
, struct blk_mq_hw_ctx
, delayed_work
.work
);
716 __blk_mq_run_hw_queue(hctx
);
719 static void __blk_mq_insert_request(struct blk_mq_hw_ctx
*hctx
,
722 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
;
724 trace_block_rq_insert(hctx
->queue
, rq
);
726 list_add_tail(&rq
->queuelist
, &ctx
->rq_list
);
727 blk_mq_hctx_mark_pending(hctx
, ctx
);
730 * We do this early, to ensure we are on the right CPU.
732 blk_mq_add_timer(rq
);
735 void blk_mq_insert_request(struct request_queue
*q
, struct request
*rq
,
738 struct blk_mq_hw_ctx
*hctx
;
739 struct blk_mq_ctx
*ctx
, *current_ctx
;
742 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
744 if (rq
->cmd_flags
& (REQ_FLUSH
| REQ_FUA
)) {
745 blk_insert_flush(rq
);
747 current_ctx
= blk_mq_get_ctx(q
);
749 if (!cpu_online(ctx
->cpu
)) {
751 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
754 spin_lock(&ctx
->lock
);
755 __blk_mq_insert_request(hctx
, rq
);
756 spin_unlock(&ctx
->lock
);
758 blk_mq_put_ctx(current_ctx
);
762 __blk_mq_run_hw_queue(hctx
);
764 EXPORT_SYMBOL(blk_mq_insert_request
);
767 * This is a special version of blk_mq_insert_request to bypass FLUSH request
768 * check. Should only be used internally.
770 void blk_mq_run_request(struct request
*rq
, bool run_queue
, bool async
)
772 struct request_queue
*q
= rq
->q
;
773 struct blk_mq_hw_ctx
*hctx
;
774 struct blk_mq_ctx
*ctx
, *current_ctx
;
776 current_ctx
= blk_mq_get_ctx(q
);
779 if (!cpu_online(ctx
->cpu
)) {
783 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
785 /* ctx->cpu might be offline */
786 spin_lock(&ctx
->lock
);
787 __blk_mq_insert_request(hctx
, rq
);
788 spin_unlock(&ctx
->lock
);
790 blk_mq_put_ctx(current_ctx
);
793 blk_mq_run_hw_queue(hctx
, async
);
796 static void blk_mq_insert_requests(struct request_queue
*q
,
797 struct blk_mq_ctx
*ctx
,
798 struct list_head
*list
,
803 struct blk_mq_hw_ctx
*hctx
;
804 struct blk_mq_ctx
*current_ctx
;
806 trace_block_unplug(q
, depth
, !from_schedule
);
808 current_ctx
= blk_mq_get_ctx(q
);
810 if (!cpu_online(ctx
->cpu
))
812 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
815 * preemption doesn't flush plug list, so it's possible ctx->cpu is
818 spin_lock(&ctx
->lock
);
819 while (!list_empty(list
)) {
822 rq
= list_first_entry(list
, struct request
, queuelist
);
823 list_del_init(&rq
->queuelist
);
825 __blk_mq_insert_request(hctx
, rq
);
827 spin_unlock(&ctx
->lock
);
829 blk_mq_put_ctx(current_ctx
);
831 blk_mq_run_hw_queue(hctx
, from_schedule
);
834 static int plug_ctx_cmp(void *priv
, struct list_head
*a
, struct list_head
*b
)
836 struct request
*rqa
= container_of(a
, struct request
, queuelist
);
837 struct request
*rqb
= container_of(b
, struct request
, queuelist
);
839 return !(rqa
->mq_ctx
< rqb
->mq_ctx
||
840 (rqa
->mq_ctx
== rqb
->mq_ctx
&&
841 blk_rq_pos(rqa
) < blk_rq_pos(rqb
)));
844 void blk_mq_flush_plug_list(struct blk_plug
*plug
, bool from_schedule
)
846 struct blk_mq_ctx
*this_ctx
;
847 struct request_queue
*this_q
;
853 list_splice_init(&plug
->mq_list
, &list
);
855 list_sort(NULL
, &list
, plug_ctx_cmp
);
861 while (!list_empty(&list
)) {
862 rq
= list_entry_rq(list
.next
);
863 list_del_init(&rq
->queuelist
);
865 if (rq
->mq_ctx
!= this_ctx
) {
867 blk_mq_insert_requests(this_q
, this_ctx
,
872 this_ctx
= rq
->mq_ctx
;
878 list_add_tail(&rq
->queuelist
, &ctx_list
);
882 * If 'this_ctx' is set, we know we have entries to complete
883 * on 'ctx_list'. Do those.
886 blk_mq_insert_requests(this_q
, this_ctx
, &ctx_list
, depth
,
891 static void blk_mq_bio_to_request(struct request
*rq
, struct bio
*bio
)
893 init_request_from_bio(rq
, bio
);
894 blk_account_io_start(rq
, 1);
897 static void blk_mq_make_request(struct request_queue
*q
, struct bio
*bio
)
899 struct blk_mq_hw_ctx
*hctx
;
900 struct blk_mq_ctx
*ctx
;
901 const int is_sync
= rw_is_sync(bio
->bi_rw
);
902 const int is_flush_fua
= bio
->bi_rw
& (REQ_FLUSH
| REQ_FUA
);
903 int rw
= bio_data_dir(bio
);
905 unsigned int use_plug
, request_count
= 0;
908 * If we have multiple hardware queues, just go directly to
909 * one of those for sync IO.
911 use_plug
= !is_flush_fua
&& ((q
->nr_hw_queues
== 1) || !is_sync
);
913 blk_queue_bounce(q
, &bio
);
915 if (use_plug
&& blk_attempt_plug_merge(q
, bio
, &request_count
))
918 if (blk_mq_queue_enter(q
)) {
919 bio_endio(bio
, -EIO
);
923 ctx
= blk_mq_get_ctx(q
);
924 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
926 trace_block_getrq(q
, bio
, rw
);
927 rq
= __blk_mq_alloc_request(hctx
, GFP_ATOMIC
, false);
929 blk_mq_rq_ctx_init(q
, ctx
, rq
, rw
);
932 trace_block_sleeprq(q
, bio
, rw
);
933 rq
= blk_mq_alloc_request_pinned(q
, rw
, __GFP_WAIT
|GFP_ATOMIC
,
936 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
941 if (unlikely(is_flush_fua
)) {
942 blk_mq_bio_to_request(rq
, bio
);
944 blk_insert_flush(rq
);
949 * A task plug currently exists. Since this is completely lockless,
950 * utilize that to temporarily store requests until the task is
951 * either done or scheduled away.
954 struct blk_plug
*plug
= current
->plug
;
957 blk_mq_bio_to_request(rq
, bio
);
958 if (list_empty(&plug
->mq_list
))
960 else if (request_count
>= BLK_MAX_REQUEST_COUNT
) {
961 blk_flush_plug_list(plug
, false);
964 list_add_tail(&rq
->queuelist
, &plug
->mq_list
);
970 spin_lock(&ctx
->lock
);
972 if ((hctx
->flags
& BLK_MQ_F_SHOULD_MERGE
) &&
973 blk_mq_attempt_merge(q
, ctx
, bio
))
974 __blk_mq_free_request(hctx
, ctx
, rq
);
976 blk_mq_bio_to_request(rq
, bio
);
977 __blk_mq_insert_request(hctx
, rq
);
980 spin_unlock(&ctx
->lock
);
984 * For a SYNC request, send it to the hardware immediately. For an
985 * ASYNC request, just ensure that we run it later on. The latter
986 * allows for merging opportunities and more efficient dispatching.
989 blk_mq_run_hw_queue(hctx
, !is_sync
|| is_flush_fua
);
993 * Default mapping to a software queue, since we use one per CPU.
995 struct blk_mq_hw_ctx
*blk_mq_map_queue(struct request_queue
*q
, const int cpu
)
997 return q
->queue_hw_ctx
[q
->mq_map
[cpu
]];
999 EXPORT_SYMBOL(blk_mq_map_queue
);
1001 struct blk_mq_hw_ctx
*blk_mq_alloc_single_hw_queue(struct blk_mq_reg
*reg
,
1002 unsigned int hctx_index
)
1004 return kmalloc_node(sizeof(struct blk_mq_hw_ctx
),
1005 GFP_KERNEL
| __GFP_ZERO
, reg
->numa_node
);
1007 EXPORT_SYMBOL(blk_mq_alloc_single_hw_queue
);
1009 void blk_mq_free_single_hw_queue(struct blk_mq_hw_ctx
*hctx
,
1010 unsigned int hctx_index
)
1014 EXPORT_SYMBOL(blk_mq_free_single_hw_queue
);
1016 static void blk_mq_hctx_notify(void *data
, unsigned long action
,
1019 struct blk_mq_hw_ctx
*hctx
= data
;
1020 struct blk_mq_ctx
*ctx
;
1023 if (action
!= CPU_DEAD
&& action
!= CPU_DEAD_FROZEN
)
1027 * Move ctx entries to new CPU, if this one is going away.
1029 ctx
= __blk_mq_get_ctx(hctx
->queue
, cpu
);
1031 spin_lock(&ctx
->lock
);
1032 if (!list_empty(&ctx
->rq_list
)) {
1033 list_splice_init(&ctx
->rq_list
, &tmp
);
1034 clear_bit(ctx
->index_hw
, hctx
->ctx_map
);
1036 spin_unlock(&ctx
->lock
);
1038 if (list_empty(&tmp
))
1041 ctx
= blk_mq_get_ctx(hctx
->queue
);
1042 spin_lock(&ctx
->lock
);
1044 while (!list_empty(&tmp
)) {
1047 rq
= list_first_entry(&tmp
, struct request
, queuelist
);
1049 list_move_tail(&rq
->queuelist
, &ctx
->rq_list
);
1052 blk_mq_hctx_mark_pending(hctx
, ctx
);
1054 spin_unlock(&ctx
->lock
);
1055 blk_mq_put_ctx(ctx
);
1058 static void blk_mq_init_hw_commands(struct blk_mq_hw_ctx
*hctx
,
1059 void (*init
)(void *, struct blk_mq_hw_ctx
*,
1060 struct request
*, unsigned int),
1065 for (i
= 0; i
< hctx
->queue_depth
; i
++) {
1066 struct request
*rq
= hctx
->rqs
[i
];
1068 init(data
, hctx
, rq
, i
);
1072 void blk_mq_init_commands(struct request_queue
*q
,
1073 void (*init
)(void *, struct blk_mq_hw_ctx
*,
1074 struct request
*, unsigned int),
1077 struct blk_mq_hw_ctx
*hctx
;
1080 queue_for_each_hw_ctx(q
, hctx
, i
)
1081 blk_mq_init_hw_commands(hctx
, init
, data
);
1083 EXPORT_SYMBOL(blk_mq_init_commands
);
1085 static void blk_mq_free_rq_map(struct blk_mq_hw_ctx
*hctx
)
1089 while (!list_empty(&hctx
->page_list
)) {
1090 page
= list_first_entry(&hctx
->page_list
, struct page
, list
);
1091 list_del_init(&page
->list
);
1092 __free_pages(page
, page
->private);
1098 blk_mq_free_tags(hctx
->tags
);
1101 static size_t order_to_size(unsigned int order
)
1103 size_t ret
= PAGE_SIZE
;
1111 static int blk_mq_init_rq_map(struct blk_mq_hw_ctx
*hctx
,
1112 unsigned int reserved_tags
, int node
)
1114 unsigned int i
, j
, entries_per_page
, max_order
= 4;
1115 size_t rq_size
, left
;
1117 INIT_LIST_HEAD(&hctx
->page_list
);
1119 hctx
->rqs
= kmalloc_node(hctx
->queue_depth
* sizeof(struct request
*),
1125 * rq_size is the size of the request plus driver payload, rounded
1126 * to the cacheline size
1128 rq_size
= round_up(sizeof(struct request
) + hctx
->cmd_size
,
1130 left
= rq_size
* hctx
->queue_depth
;
1132 for (i
= 0; i
< hctx
->queue_depth
;) {
1133 int this_order
= max_order
;
1138 while (left
< order_to_size(this_order
- 1) && this_order
)
1142 page
= alloc_pages_node(node
, GFP_KERNEL
, this_order
);
1147 if (order_to_size(this_order
) < rq_size
)
1154 page
->private = this_order
;
1155 list_add_tail(&page
->list
, &hctx
->page_list
);
1157 p
= page_address(page
);
1158 entries_per_page
= order_to_size(this_order
) / rq_size
;
1159 to_do
= min(entries_per_page
, hctx
->queue_depth
- i
);
1160 left
-= to_do
* rq_size
;
1161 for (j
= 0; j
< to_do
; j
++) {
1163 blk_mq_rq_init(hctx
, hctx
->rqs
[i
]);
1169 if (i
< (reserved_tags
+ BLK_MQ_TAG_MIN
))
1171 else if (i
!= hctx
->queue_depth
) {
1172 hctx
->queue_depth
= i
;
1173 pr_warn("%s: queue depth set to %u because of low memory\n",
1177 hctx
->tags
= blk_mq_init_tags(hctx
->queue_depth
, reserved_tags
, node
);
1180 blk_mq_free_rq_map(hctx
);
1187 static int blk_mq_init_hw_queues(struct request_queue
*q
,
1188 struct blk_mq_reg
*reg
, void *driver_data
)
1190 struct blk_mq_hw_ctx
*hctx
;
1194 * Initialize hardware queues
1196 queue_for_each_hw_ctx(q
, hctx
, i
) {
1197 unsigned int num_maps
;
1200 node
= hctx
->numa_node
;
1201 if (node
== NUMA_NO_NODE
)
1202 node
= hctx
->numa_node
= reg
->numa_node
;
1204 INIT_DELAYED_WORK(&hctx
->delayed_work
, blk_mq_work_fn
);
1205 spin_lock_init(&hctx
->lock
);
1206 INIT_LIST_HEAD(&hctx
->dispatch
);
1208 hctx
->queue_num
= i
;
1209 hctx
->flags
= reg
->flags
;
1210 hctx
->queue_depth
= reg
->queue_depth
;
1211 hctx
->cmd_size
= reg
->cmd_size
;
1213 blk_mq_init_cpu_notifier(&hctx
->cpu_notifier
,
1214 blk_mq_hctx_notify
, hctx
);
1215 blk_mq_register_cpu_notifier(&hctx
->cpu_notifier
);
1217 if (blk_mq_init_rq_map(hctx
, reg
->reserved_tags
, node
))
1221 * Allocate space for all possible cpus to avoid allocation in
1224 hctx
->ctxs
= kmalloc_node(nr_cpu_ids
* sizeof(void *),
1229 num_maps
= ALIGN(nr_cpu_ids
, BITS_PER_LONG
) / BITS_PER_LONG
;
1230 hctx
->ctx_map
= kzalloc_node(num_maps
* sizeof(unsigned long),
1235 hctx
->nr_ctx_map
= num_maps
;
1238 if (reg
->ops
->init_hctx
&&
1239 reg
->ops
->init_hctx(hctx
, driver_data
, i
))
1243 if (i
== q
->nr_hw_queues
)
1249 queue_for_each_hw_ctx(q
, hctx
, j
) {
1253 if (reg
->ops
->exit_hctx
)
1254 reg
->ops
->exit_hctx(hctx
, j
);
1256 blk_mq_unregister_cpu_notifier(&hctx
->cpu_notifier
);
1257 blk_mq_free_rq_map(hctx
);
1264 static void blk_mq_init_cpu_queues(struct request_queue
*q
,
1265 unsigned int nr_hw_queues
)
1269 for_each_possible_cpu(i
) {
1270 struct blk_mq_ctx
*__ctx
= per_cpu_ptr(q
->queue_ctx
, i
);
1271 struct blk_mq_hw_ctx
*hctx
;
1273 memset(__ctx
, 0, sizeof(*__ctx
));
1275 spin_lock_init(&__ctx
->lock
);
1276 INIT_LIST_HEAD(&__ctx
->rq_list
);
1279 /* If the cpu isn't online, the cpu is mapped to first hctx */
1280 hctx
= q
->mq_ops
->map_queue(q
, i
);
1287 * Set local node, IFF we have more than one hw queue. If
1288 * not, we remain on the home node of the device
1290 if (nr_hw_queues
> 1 && hctx
->numa_node
== NUMA_NO_NODE
)
1291 hctx
->numa_node
= cpu_to_node(i
);
1295 static void blk_mq_map_swqueue(struct request_queue
*q
)
1298 struct blk_mq_hw_ctx
*hctx
;
1299 struct blk_mq_ctx
*ctx
;
1301 queue_for_each_hw_ctx(q
, hctx
, i
) {
1306 * Map software to hardware queues
1308 queue_for_each_ctx(q
, ctx
, i
) {
1309 /* If the cpu isn't online, the cpu is mapped to first hctx */
1310 hctx
= q
->mq_ops
->map_queue(q
, i
);
1311 ctx
->index_hw
= hctx
->nr_ctx
;
1312 hctx
->ctxs
[hctx
->nr_ctx
++] = ctx
;
1316 struct request_queue
*blk_mq_init_queue(struct blk_mq_reg
*reg
,
1319 struct blk_mq_hw_ctx
**hctxs
;
1320 struct blk_mq_ctx
*ctx
;
1321 struct request_queue
*q
;
1324 if (!reg
->nr_hw_queues
||
1325 !reg
->ops
->queue_rq
|| !reg
->ops
->map_queue
||
1326 !reg
->ops
->alloc_hctx
|| !reg
->ops
->free_hctx
)
1327 return ERR_PTR(-EINVAL
);
1329 if (!reg
->queue_depth
)
1330 reg
->queue_depth
= BLK_MQ_MAX_DEPTH
;
1331 else if (reg
->queue_depth
> BLK_MQ_MAX_DEPTH
) {
1332 pr_err("blk-mq: queuedepth too large (%u)\n", reg
->queue_depth
);
1333 reg
->queue_depth
= BLK_MQ_MAX_DEPTH
;
1337 * Set aside a tag for flush requests. It will only be used while
1338 * another flush request is in progress but outside the driver.
1340 * TODO: only allocate if flushes are supported
1343 reg
->reserved_tags
++;
1345 if (reg
->queue_depth
< (reg
->reserved_tags
+ BLK_MQ_TAG_MIN
))
1346 return ERR_PTR(-EINVAL
);
1348 ctx
= alloc_percpu(struct blk_mq_ctx
);
1350 return ERR_PTR(-ENOMEM
);
1352 hctxs
= kmalloc_node(reg
->nr_hw_queues
* sizeof(*hctxs
), GFP_KERNEL
,
1358 for (i
= 0; i
< reg
->nr_hw_queues
; i
++) {
1359 hctxs
[i
] = reg
->ops
->alloc_hctx(reg
, i
);
1363 hctxs
[i
]->numa_node
= NUMA_NO_NODE
;
1364 hctxs
[i
]->queue_num
= i
;
1367 q
= blk_alloc_queue_node(GFP_KERNEL
, reg
->numa_node
);
1371 q
->mq_map
= blk_mq_make_queue_map(reg
);
1375 setup_timer(&q
->timeout
, blk_mq_rq_timer
, (unsigned long) q
);
1376 blk_queue_rq_timeout(q
, 30000);
1378 q
->nr_queues
= nr_cpu_ids
;
1379 q
->nr_hw_queues
= reg
->nr_hw_queues
;
1382 q
->queue_hw_ctx
= hctxs
;
1384 q
->mq_ops
= reg
->ops
;
1385 q
->queue_flags
|= QUEUE_FLAG_MQ_DEFAULT
;
1387 blk_queue_make_request(q
, blk_mq_make_request
);
1388 blk_queue_rq_timed_out(q
, reg
->ops
->timeout
);
1390 blk_queue_rq_timeout(q
, reg
->timeout
);
1392 blk_mq_init_flush(q
);
1393 blk_mq_init_cpu_queues(q
, reg
->nr_hw_queues
);
1395 if (blk_mq_init_hw_queues(q
, reg
, driver_data
))
1398 blk_mq_map_swqueue(q
);
1400 mutex_lock(&all_q_mutex
);
1401 list_add_tail(&q
->all_q_node
, &all_q_list
);
1402 mutex_unlock(&all_q_mutex
);
1408 blk_cleanup_queue(q
);
1410 for (i
= 0; i
< reg
->nr_hw_queues
; i
++) {
1413 reg
->ops
->free_hctx(hctxs
[i
], i
);
1418 return ERR_PTR(-ENOMEM
);
1420 EXPORT_SYMBOL(blk_mq_init_queue
);
1422 void blk_mq_free_queue(struct request_queue
*q
)
1424 struct blk_mq_hw_ctx
*hctx
;
1427 queue_for_each_hw_ctx(q
, hctx
, i
) {
1428 cancel_delayed_work_sync(&hctx
->delayed_work
);
1429 kfree(hctx
->ctx_map
);
1431 blk_mq_free_rq_map(hctx
);
1432 blk_mq_unregister_cpu_notifier(&hctx
->cpu_notifier
);
1433 if (q
->mq_ops
->exit_hctx
)
1434 q
->mq_ops
->exit_hctx(hctx
, i
);
1435 q
->mq_ops
->free_hctx(hctx
, i
);
1438 free_percpu(q
->queue_ctx
);
1439 kfree(q
->queue_hw_ctx
);
1442 q
->queue_ctx
= NULL
;
1443 q
->queue_hw_ctx
= NULL
;
1446 mutex_lock(&all_q_mutex
);
1447 list_del_init(&q
->all_q_node
);
1448 mutex_unlock(&all_q_mutex
);
1450 EXPORT_SYMBOL(blk_mq_free_queue
);
1452 /* Basically redo blk_mq_init_queue with queue frozen */
1453 static void blk_mq_queue_reinit(struct request_queue
*q
)
1455 blk_mq_freeze_queue(q
);
1457 blk_mq_update_queue_map(q
->mq_map
, q
->nr_hw_queues
);
1460 * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
1461 * we should change hctx numa_node according to new topology (this
1462 * involves free and re-allocate memory, worthy doing?)
1465 blk_mq_map_swqueue(q
);
1467 blk_mq_unfreeze_queue(q
);
1470 static int blk_mq_queue_reinit_notify(struct notifier_block
*nb
,
1471 unsigned long action
, void *hcpu
)
1473 struct request_queue
*q
;
1476 * Before new mapping is established, hotadded cpu might already start
1477 * handling requests. This doesn't break anything as we map offline
1478 * CPUs to first hardware queue. We will re-init queue below to get
1481 if (action
!= CPU_DEAD
&& action
!= CPU_DEAD_FROZEN
&&
1482 action
!= CPU_ONLINE
&& action
!= CPU_ONLINE_FROZEN
)
1485 mutex_lock(&all_q_mutex
);
1486 list_for_each_entry(q
, &all_q_list
, all_q_node
)
1487 blk_mq_queue_reinit(q
);
1488 mutex_unlock(&all_q_mutex
);
1492 static int __init
blk_mq_init(void)
1496 for_each_possible_cpu(i
)
1497 init_llist_head(&per_cpu(ipi_lists
, i
));
1501 /* Must be called after percpu_counter_hotcpu_callback() */
1502 hotcpu_notifier(blk_mq_queue_reinit_notify
, -10);
1506 subsys_initcall(blk_mq_init
);