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
);
208 if (!(gfp
& __GFP_WAIT
))
211 __blk_mq_run_hw_queue(hctx
);
212 blk_mq_wait_for_tags(hctx
->tags
);
218 struct request
*blk_mq_alloc_request(struct request_queue
*q
, int rw
,
219 gfp_t gfp
, bool reserved
)
223 if (blk_mq_queue_enter(q
))
226 rq
= blk_mq_alloc_request_pinned(q
, rw
, gfp
, reserved
);
228 blk_mq_put_ctx(rq
->mq_ctx
);
232 struct request
*blk_mq_alloc_reserved_request(struct request_queue
*q
, int rw
,
237 if (blk_mq_queue_enter(q
))
240 rq
= blk_mq_alloc_request_pinned(q
, rw
, gfp
, true);
242 blk_mq_put_ctx(rq
->mq_ctx
);
245 EXPORT_SYMBOL(blk_mq_alloc_reserved_request
);
248 * Re-init and set pdu, if we have it
250 static void blk_mq_rq_init(struct blk_mq_hw_ctx
*hctx
, struct request
*rq
)
252 blk_rq_init(hctx
->queue
, rq
);
255 rq
->special
= blk_mq_rq_to_pdu(rq
);
258 static void __blk_mq_free_request(struct blk_mq_hw_ctx
*hctx
,
259 struct blk_mq_ctx
*ctx
, struct request
*rq
)
261 const int tag
= rq
->tag
;
262 struct request_queue
*q
= rq
->q
;
264 blk_mq_rq_init(hctx
, rq
);
265 blk_mq_put_tag(hctx
->tags
, tag
);
267 blk_mq_queue_exit(q
);
270 void blk_mq_free_request(struct request
*rq
)
272 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
;
273 struct blk_mq_hw_ctx
*hctx
;
274 struct request_queue
*q
= rq
->q
;
276 ctx
->rq_completed
[rq_is_sync(rq
)]++;
278 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
279 __blk_mq_free_request(hctx
, ctx
, rq
);
282 static void blk_mq_bio_endio(struct request
*rq
, struct bio
*bio
, int error
)
285 clear_bit(BIO_UPTODATE
, &bio
->bi_flags
);
286 else if (!test_bit(BIO_UPTODATE
, &bio
->bi_flags
))
289 if (unlikely(rq
->cmd_flags
& REQ_QUIET
))
290 set_bit(BIO_QUIET
, &bio
->bi_flags
);
292 /* don't actually finish bio if it's part of flush sequence */
293 if (!(rq
->cmd_flags
& REQ_FLUSH_SEQ
))
294 bio_endio(bio
, error
);
297 void blk_mq_complete_request(struct request
*rq
, int error
)
299 struct bio
*bio
= rq
->bio
;
300 unsigned int bytes
= 0;
302 trace_block_rq_complete(rq
->q
, rq
);
305 struct bio
*next
= bio
->bi_next
;
308 bytes
+= bio
->bi_size
;
309 blk_mq_bio_endio(rq
, bio
, error
);
313 blk_account_io_completion(rq
, bytes
);
315 blk_account_io_done(rq
);
318 rq
->end_io(rq
, error
);
320 blk_mq_free_request(rq
);
323 void __blk_mq_end_io(struct request
*rq
, int error
)
325 if (!blk_mark_rq_complete(rq
))
326 blk_mq_complete_request(rq
, error
);
329 #if defined(CONFIG_SMP)
332 * Called with interrupts disabled.
334 static void ipi_end_io(void *data
)
336 struct llist_head
*list
= &per_cpu(ipi_lists
, smp_processor_id());
337 struct llist_node
*entry
, *next
;
340 entry
= llist_del_all(list
);
344 rq
= llist_entry(entry
, struct request
, ll_list
);
345 __blk_mq_end_io(rq
, rq
->errors
);
350 static int ipi_remote_cpu(struct blk_mq_ctx
*ctx
, const int cpu
,
351 struct request
*rq
, const int error
)
353 struct call_single_data
*data
= &rq
->csd
;
356 rq
->ll_list
.next
= NULL
;
359 * If the list is non-empty, an existing IPI must already
360 * be "in flight". If that is the case, we need not schedule
363 if (llist_add(&rq
->ll_list
, &per_cpu(ipi_lists
, ctx
->cpu
))) {
364 data
->func
= ipi_end_io
;
366 __smp_call_function_single(ctx
->cpu
, data
, 0);
371 #else /* CONFIG_SMP */
372 static int ipi_remote_cpu(struct blk_mq_ctx
*ctx
, const int cpu
,
373 struct request
*rq
, const int error
)
380 * End IO on this request on a multiqueue enabled driver. We'll either do
381 * it directly inline, or punt to a local IPI handler on the matching
384 void blk_mq_end_io(struct request
*rq
, int error
)
386 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
;
389 if (!ctx
->ipi_redirect
)
390 return __blk_mq_end_io(rq
, error
);
394 if (cpu
== ctx
->cpu
|| !cpu_online(ctx
->cpu
) ||
395 !ipi_remote_cpu(ctx
, cpu
, rq
, error
))
396 __blk_mq_end_io(rq
, error
);
400 EXPORT_SYMBOL(blk_mq_end_io
);
402 static void blk_mq_start_request(struct request
*rq
)
404 struct request_queue
*q
= rq
->q
;
406 trace_block_rq_issue(q
, rq
);
409 * Just mark start time and set the started bit. Due to memory
410 * ordering, we know we'll see the correct deadline as long as
411 * REQ_ATOMIC_STARTED is seen.
413 rq
->deadline
= jiffies
+ q
->rq_timeout
;
414 set_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
);
417 static void blk_mq_requeue_request(struct request
*rq
)
419 struct request_queue
*q
= rq
->q
;
421 trace_block_rq_requeue(q
, rq
);
422 clear_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
);
425 struct blk_mq_timeout_data
{
426 struct blk_mq_hw_ctx
*hctx
;
428 unsigned int *next_set
;
431 static void blk_mq_timeout_check(void *__data
, unsigned long *free_tags
)
433 struct blk_mq_timeout_data
*data
= __data
;
434 struct blk_mq_hw_ctx
*hctx
= data
->hctx
;
437 /* It may not be in flight yet (this is where
438 * the REQ_ATOMIC_STARTED flag comes in). The requests are
439 * statically allocated, so we know it's always safe to access the
440 * memory associated with a bit offset into ->rqs[].
446 tag
= find_next_zero_bit(free_tags
, hctx
->queue_depth
, tag
);
447 if (tag
>= hctx
->queue_depth
)
450 rq
= hctx
->rqs
[tag
++];
452 if (!test_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
))
455 blk_rq_check_expired(rq
, data
->next
, data
->next_set
);
459 static void blk_mq_hw_ctx_check_timeout(struct blk_mq_hw_ctx
*hctx
,
461 unsigned int *next_set
)
463 struct blk_mq_timeout_data data
= {
466 .next_set
= next_set
,
470 * Ask the tagging code to iterate busy requests, so we can
471 * check them for timeout.
473 blk_mq_tag_busy_iter(hctx
->tags
, blk_mq_timeout_check
, &data
);
476 static void blk_mq_rq_timer(unsigned long data
)
478 struct request_queue
*q
= (struct request_queue
*) data
;
479 struct blk_mq_hw_ctx
*hctx
;
480 unsigned long next
= 0;
483 queue_for_each_hw_ctx(q
, hctx
, i
)
484 blk_mq_hw_ctx_check_timeout(hctx
, &next
, &next_set
);
487 mod_timer(&q
->timeout
, round_jiffies_up(next
));
491 * Reverse check our software queue for entries that we could potentially
492 * merge with. Currently includes a hand-wavy stop count of 8, to not spend
493 * too much time checking for merges.
495 static bool blk_mq_attempt_merge(struct request_queue
*q
,
496 struct blk_mq_ctx
*ctx
, struct bio
*bio
)
501 list_for_each_entry_reverse(rq
, &ctx
->rq_list
, queuelist
) {
507 if (!blk_rq_merge_ok(rq
, bio
))
510 el_ret
= blk_try_merge(rq
, bio
);
511 if (el_ret
== ELEVATOR_BACK_MERGE
) {
512 if (bio_attempt_back_merge(q
, rq
, bio
)) {
517 } else if (el_ret
== ELEVATOR_FRONT_MERGE
) {
518 if (bio_attempt_front_merge(q
, rq
, bio
)) {
529 void blk_mq_add_timer(struct request
*rq
)
531 __blk_add_timer(rq
, NULL
);
535 * Run this hardware queue, pulling any software queues mapped to it in.
536 * Note that this function currently has various problems around ordering
537 * of IO. In particular, we'd like FIFO behaviour on handling existing
538 * items on the hctx->dispatch list. Ignore that for now.
540 static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx
*hctx
)
542 struct request_queue
*q
= hctx
->queue
;
543 struct blk_mq_ctx
*ctx
;
548 if (unlikely(test_bit(BLK_MQ_S_STOPPED
, &hctx
->flags
)))
554 * Touch any software queue that has pending entries.
556 for_each_set_bit(bit
, hctx
->ctx_map
, hctx
->nr_ctx
) {
557 clear_bit(bit
, hctx
->ctx_map
);
558 ctx
= hctx
->ctxs
[bit
];
559 BUG_ON(bit
!= ctx
->index_hw
);
561 spin_lock(&ctx
->lock
);
562 list_splice_tail_init(&ctx
->rq_list
, &rq_list
);
563 spin_unlock(&ctx
->lock
);
567 * If we have previous entries on our dispatch list, grab them
568 * and stuff them at the front for more fair dispatch.
570 if (!list_empty_careful(&hctx
->dispatch
)) {
571 spin_lock(&hctx
->lock
);
572 if (!list_empty(&hctx
->dispatch
))
573 list_splice_init(&hctx
->dispatch
, &rq_list
);
574 spin_unlock(&hctx
->lock
);
578 * Delete and return all entries from our dispatch list
583 * Now process all the entries, sending them to the driver.
585 while (!list_empty(&rq_list
)) {
588 rq
= list_first_entry(&rq_list
, struct request
, queuelist
);
589 list_del_init(&rq
->queuelist
);
590 blk_mq_start_request(rq
);
593 * Last request in the series. Flag it as such, this
594 * enables drivers to know when IO should be kicked off,
595 * if they don't do it on a per-request basis.
597 * Note: the flag isn't the only condition drivers
598 * should do kick off. If drive is busy, the last
599 * request might not have the bit set.
601 if (list_empty(&rq_list
))
602 rq
->cmd_flags
|= REQ_END
;
604 ret
= q
->mq_ops
->queue_rq(hctx
, rq
);
606 case BLK_MQ_RQ_QUEUE_OK
:
609 case BLK_MQ_RQ_QUEUE_BUSY
:
611 * FIXME: we should have a mechanism to stop the queue
612 * like blk_stop_queue, otherwise we will waste cpu
615 list_add(&rq
->queuelist
, &rq_list
);
616 blk_mq_requeue_request(rq
);
619 pr_err("blk-mq: bad return on queue: %d\n", ret
);
621 case BLK_MQ_RQ_QUEUE_ERROR
:
622 blk_mq_end_io(rq
, rq
->errors
);
626 if (ret
== BLK_MQ_RQ_QUEUE_BUSY
)
631 hctx
->dispatched
[0]++;
632 else if (queued
< (1 << (BLK_MQ_MAX_DISPATCH_ORDER
- 1)))
633 hctx
->dispatched
[ilog2(queued
) + 1]++;
636 * Any items that need requeuing? Stuff them into hctx->dispatch,
637 * that is where we will continue on next queue run.
639 if (!list_empty(&rq_list
)) {
640 spin_lock(&hctx
->lock
);
641 list_splice(&rq_list
, &hctx
->dispatch
);
642 spin_unlock(&hctx
->lock
);
646 void blk_mq_run_hw_queue(struct blk_mq_hw_ctx
*hctx
, bool async
)
648 if (unlikely(test_bit(BLK_MQ_S_STOPPED
, &hctx
->flags
)))
652 __blk_mq_run_hw_queue(hctx
);
654 struct request_queue
*q
= hctx
->queue
;
656 kblockd_schedule_delayed_work(q
, &hctx
->delayed_work
, 0);
660 void blk_mq_run_queues(struct request_queue
*q
, bool async
)
662 struct blk_mq_hw_ctx
*hctx
;
665 queue_for_each_hw_ctx(q
, hctx
, i
) {
666 if ((!blk_mq_hctx_has_pending(hctx
) &&
667 list_empty_careful(&hctx
->dispatch
)) ||
668 test_bit(BLK_MQ_S_STOPPED
, &hctx
->flags
))
671 blk_mq_run_hw_queue(hctx
, async
);
674 EXPORT_SYMBOL(blk_mq_run_queues
);
676 void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx
*hctx
)
678 cancel_delayed_work(&hctx
->delayed_work
);
679 set_bit(BLK_MQ_S_STOPPED
, &hctx
->state
);
681 EXPORT_SYMBOL(blk_mq_stop_hw_queue
);
683 void blk_mq_stop_hw_queues(struct request_queue
*q
)
685 struct blk_mq_hw_ctx
*hctx
;
688 queue_for_each_hw_ctx(q
, hctx
, i
)
689 blk_mq_stop_hw_queue(hctx
);
691 EXPORT_SYMBOL(blk_mq_stop_hw_queues
);
693 void blk_mq_start_hw_queue(struct blk_mq_hw_ctx
*hctx
)
695 clear_bit(BLK_MQ_S_STOPPED
, &hctx
->state
);
696 __blk_mq_run_hw_queue(hctx
);
698 EXPORT_SYMBOL(blk_mq_start_hw_queue
);
700 void blk_mq_start_stopped_hw_queues(struct request_queue
*q
)
702 struct blk_mq_hw_ctx
*hctx
;
705 queue_for_each_hw_ctx(q
, hctx
, i
) {
706 if (!test_bit(BLK_MQ_S_STOPPED
, &hctx
->state
))
709 clear_bit(BLK_MQ_S_STOPPED
, &hctx
->state
);
710 blk_mq_run_hw_queue(hctx
, true);
713 EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues
);
715 static void blk_mq_work_fn(struct work_struct
*work
)
717 struct blk_mq_hw_ctx
*hctx
;
719 hctx
= container_of(work
, struct blk_mq_hw_ctx
, delayed_work
.work
);
720 __blk_mq_run_hw_queue(hctx
);
723 static void __blk_mq_insert_request(struct blk_mq_hw_ctx
*hctx
,
726 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
;
728 trace_block_rq_insert(hctx
->queue
, rq
);
730 list_add_tail(&rq
->queuelist
, &ctx
->rq_list
);
731 blk_mq_hctx_mark_pending(hctx
, ctx
);
734 * We do this early, to ensure we are on the right CPU.
736 blk_mq_add_timer(rq
);
739 void blk_mq_insert_request(struct request_queue
*q
, struct request
*rq
,
742 struct blk_mq_hw_ctx
*hctx
;
743 struct blk_mq_ctx
*ctx
, *current_ctx
;
746 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
748 if (rq
->cmd_flags
& (REQ_FLUSH
| REQ_FUA
)) {
749 blk_insert_flush(rq
);
751 current_ctx
= blk_mq_get_ctx(q
);
753 if (!cpu_online(ctx
->cpu
)) {
755 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
758 spin_lock(&ctx
->lock
);
759 __blk_mq_insert_request(hctx
, rq
);
760 spin_unlock(&ctx
->lock
);
762 blk_mq_put_ctx(current_ctx
);
766 __blk_mq_run_hw_queue(hctx
);
768 EXPORT_SYMBOL(blk_mq_insert_request
);
771 * This is a special version of blk_mq_insert_request to bypass FLUSH request
772 * check. Should only be used internally.
774 void blk_mq_run_request(struct request
*rq
, bool run_queue
, bool async
)
776 struct request_queue
*q
= rq
->q
;
777 struct blk_mq_hw_ctx
*hctx
;
778 struct blk_mq_ctx
*ctx
, *current_ctx
;
780 current_ctx
= blk_mq_get_ctx(q
);
783 if (!cpu_online(ctx
->cpu
)) {
787 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
789 /* ctx->cpu might be offline */
790 spin_lock(&ctx
->lock
);
791 __blk_mq_insert_request(hctx
, rq
);
792 spin_unlock(&ctx
->lock
);
794 blk_mq_put_ctx(current_ctx
);
797 blk_mq_run_hw_queue(hctx
, async
);
800 static void blk_mq_insert_requests(struct request_queue
*q
,
801 struct blk_mq_ctx
*ctx
,
802 struct list_head
*list
,
807 struct blk_mq_hw_ctx
*hctx
;
808 struct blk_mq_ctx
*current_ctx
;
810 trace_block_unplug(q
, depth
, !from_schedule
);
812 current_ctx
= blk_mq_get_ctx(q
);
814 if (!cpu_online(ctx
->cpu
))
816 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
819 * preemption doesn't flush plug list, so it's possible ctx->cpu is
822 spin_lock(&ctx
->lock
);
823 while (!list_empty(list
)) {
826 rq
= list_first_entry(list
, struct request
, queuelist
);
827 list_del_init(&rq
->queuelist
);
829 __blk_mq_insert_request(hctx
, rq
);
831 spin_unlock(&ctx
->lock
);
833 blk_mq_put_ctx(current_ctx
);
835 blk_mq_run_hw_queue(hctx
, from_schedule
);
838 static int plug_ctx_cmp(void *priv
, struct list_head
*a
, struct list_head
*b
)
840 struct request
*rqa
= container_of(a
, struct request
, queuelist
);
841 struct request
*rqb
= container_of(b
, struct request
, queuelist
);
843 return !(rqa
->mq_ctx
< rqb
->mq_ctx
||
844 (rqa
->mq_ctx
== rqb
->mq_ctx
&&
845 blk_rq_pos(rqa
) < blk_rq_pos(rqb
)));
848 void blk_mq_flush_plug_list(struct blk_plug
*plug
, bool from_schedule
)
850 struct blk_mq_ctx
*this_ctx
;
851 struct request_queue
*this_q
;
857 list_splice_init(&plug
->mq_list
, &list
);
859 list_sort(NULL
, &list
, plug_ctx_cmp
);
865 while (!list_empty(&list
)) {
866 rq
= list_entry_rq(list
.next
);
867 list_del_init(&rq
->queuelist
);
869 if (rq
->mq_ctx
!= this_ctx
) {
871 blk_mq_insert_requests(this_q
, this_ctx
,
876 this_ctx
= rq
->mq_ctx
;
882 list_add_tail(&rq
->queuelist
, &ctx_list
);
886 * If 'this_ctx' is set, we know we have entries to complete
887 * on 'ctx_list'. Do those.
890 blk_mq_insert_requests(this_q
, this_ctx
, &ctx_list
, depth
,
895 static void blk_mq_bio_to_request(struct request
*rq
, struct bio
*bio
)
897 init_request_from_bio(rq
, bio
);
898 blk_account_io_start(rq
, 1);
901 static void blk_mq_make_request(struct request_queue
*q
, struct bio
*bio
)
903 struct blk_mq_hw_ctx
*hctx
;
904 struct blk_mq_ctx
*ctx
;
905 const int is_sync
= rw_is_sync(bio
->bi_rw
);
906 const int is_flush_fua
= bio
->bi_rw
& (REQ_FLUSH
| REQ_FUA
);
907 int rw
= bio_data_dir(bio
);
909 unsigned int use_plug
, request_count
= 0;
912 * If we have multiple hardware queues, just go directly to
913 * one of those for sync IO.
915 use_plug
= !is_flush_fua
&& ((q
->nr_hw_queues
== 1) || !is_sync
);
917 blk_queue_bounce(q
, &bio
);
919 if (use_plug
&& blk_attempt_plug_merge(q
, bio
, &request_count
))
922 if (blk_mq_queue_enter(q
)) {
923 bio_endio(bio
, -EIO
);
927 ctx
= blk_mq_get_ctx(q
);
928 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
930 trace_block_getrq(q
, bio
, rw
);
931 rq
= __blk_mq_alloc_request(hctx
, GFP_ATOMIC
, false);
933 blk_mq_rq_ctx_init(q
, ctx
, rq
, rw
);
936 trace_block_sleeprq(q
, bio
, rw
);
937 rq
= blk_mq_alloc_request_pinned(q
, rw
, __GFP_WAIT
|GFP_ATOMIC
,
940 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
945 if (unlikely(is_flush_fua
)) {
946 blk_mq_bio_to_request(rq
, bio
);
948 blk_insert_flush(rq
);
953 * A task plug currently exists. Since this is completely lockless,
954 * utilize that to temporarily store requests until the task is
955 * either done or scheduled away.
958 struct blk_plug
*plug
= current
->plug
;
961 blk_mq_bio_to_request(rq
, bio
);
962 if (list_empty(&plug
->mq_list
))
964 else if (request_count
>= BLK_MAX_REQUEST_COUNT
) {
965 blk_flush_plug_list(plug
, false);
968 list_add_tail(&rq
->queuelist
, &plug
->mq_list
);
974 spin_lock(&ctx
->lock
);
976 if ((hctx
->flags
& BLK_MQ_F_SHOULD_MERGE
) &&
977 blk_mq_attempt_merge(q
, ctx
, bio
))
978 __blk_mq_free_request(hctx
, ctx
, rq
);
980 blk_mq_bio_to_request(rq
, bio
);
981 __blk_mq_insert_request(hctx
, rq
);
984 spin_unlock(&ctx
->lock
);
988 * For a SYNC request, send it to the hardware immediately. For an
989 * ASYNC request, just ensure that we run it later on. The latter
990 * allows for merging opportunities and more efficient dispatching.
993 blk_mq_run_hw_queue(hctx
, !is_sync
|| is_flush_fua
);
997 * Default mapping to a software queue, since we use one per CPU.
999 struct blk_mq_hw_ctx
*blk_mq_map_queue(struct request_queue
*q
, const int cpu
)
1001 return q
->queue_hw_ctx
[q
->mq_map
[cpu
]];
1003 EXPORT_SYMBOL(blk_mq_map_queue
);
1005 struct blk_mq_hw_ctx
*blk_mq_alloc_single_hw_queue(struct blk_mq_reg
*reg
,
1006 unsigned int hctx_index
)
1008 return kmalloc_node(sizeof(struct blk_mq_hw_ctx
),
1009 GFP_KERNEL
| __GFP_ZERO
, reg
->numa_node
);
1011 EXPORT_SYMBOL(blk_mq_alloc_single_hw_queue
);
1013 void blk_mq_free_single_hw_queue(struct blk_mq_hw_ctx
*hctx
,
1014 unsigned int hctx_index
)
1018 EXPORT_SYMBOL(blk_mq_free_single_hw_queue
);
1020 static void blk_mq_hctx_notify(void *data
, unsigned long action
,
1023 struct blk_mq_hw_ctx
*hctx
= data
;
1024 struct blk_mq_ctx
*ctx
;
1027 if (action
!= CPU_DEAD
&& action
!= CPU_DEAD_FROZEN
)
1031 * Move ctx entries to new CPU, if this one is going away.
1033 ctx
= __blk_mq_get_ctx(hctx
->queue
, cpu
);
1035 spin_lock(&ctx
->lock
);
1036 if (!list_empty(&ctx
->rq_list
)) {
1037 list_splice_init(&ctx
->rq_list
, &tmp
);
1038 clear_bit(ctx
->index_hw
, hctx
->ctx_map
);
1040 spin_unlock(&ctx
->lock
);
1042 if (list_empty(&tmp
))
1045 ctx
= blk_mq_get_ctx(hctx
->queue
);
1046 spin_lock(&ctx
->lock
);
1048 while (!list_empty(&tmp
)) {
1051 rq
= list_first_entry(&tmp
, struct request
, queuelist
);
1053 list_move_tail(&rq
->queuelist
, &ctx
->rq_list
);
1056 blk_mq_hctx_mark_pending(hctx
, ctx
);
1058 spin_unlock(&ctx
->lock
);
1059 blk_mq_put_ctx(ctx
);
1062 static void blk_mq_init_hw_commands(struct blk_mq_hw_ctx
*hctx
,
1063 void (*init
)(void *, struct blk_mq_hw_ctx
*,
1064 struct request
*, unsigned int),
1069 for (i
= 0; i
< hctx
->queue_depth
; i
++) {
1070 struct request
*rq
= hctx
->rqs
[i
];
1072 init(data
, hctx
, rq
, i
);
1076 void blk_mq_init_commands(struct request_queue
*q
,
1077 void (*init
)(void *, struct blk_mq_hw_ctx
*,
1078 struct request
*, unsigned int),
1081 struct blk_mq_hw_ctx
*hctx
;
1084 queue_for_each_hw_ctx(q
, hctx
, i
)
1085 blk_mq_init_hw_commands(hctx
, init
, data
);
1087 EXPORT_SYMBOL(blk_mq_init_commands
);
1089 static void blk_mq_free_rq_map(struct blk_mq_hw_ctx
*hctx
)
1093 while (!list_empty(&hctx
->page_list
)) {
1094 page
= list_first_entry(&hctx
->page_list
, struct page
, list
);
1095 list_del_init(&page
->list
);
1096 __free_pages(page
, page
->private);
1102 blk_mq_free_tags(hctx
->tags
);
1105 static size_t order_to_size(unsigned int order
)
1107 size_t ret
= PAGE_SIZE
;
1115 static int blk_mq_init_rq_map(struct blk_mq_hw_ctx
*hctx
,
1116 unsigned int reserved_tags
, int node
)
1118 unsigned int i
, j
, entries_per_page
, max_order
= 4;
1119 size_t rq_size
, left
;
1121 INIT_LIST_HEAD(&hctx
->page_list
);
1123 hctx
->rqs
= kmalloc_node(hctx
->queue_depth
* sizeof(struct request
*),
1129 * rq_size is the size of the request plus driver payload, rounded
1130 * to the cacheline size
1132 rq_size
= round_up(sizeof(struct request
) + hctx
->cmd_size
,
1134 left
= rq_size
* hctx
->queue_depth
;
1136 for (i
= 0; i
< hctx
->queue_depth
;) {
1137 int this_order
= max_order
;
1142 while (left
< order_to_size(this_order
- 1) && this_order
)
1146 page
= alloc_pages_node(node
, GFP_KERNEL
, this_order
);
1151 if (order_to_size(this_order
) < rq_size
)
1158 page
->private = this_order
;
1159 list_add_tail(&page
->list
, &hctx
->page_list
);
1161 p
= page_address(page
);
1162 entries_per_page
= order_to_size(this_order
) / rq_size
;
1163 to_do
= min(entries_per_page
, hctx
->queue_depth
- i
);
1164 left
-= to_do
* rq_size
;
1165 for (j
= 0; j
< to_do
; j
++) {
1167 blk_mq_rq_init(hctx
, hctx
->rqs
[i
]);
1173 if (i
< (reserved_tags
+ BLK_MQ_TAG_MIN
))
1175 else if (i
!= hctx
->queue_depth
) {
1176 hctx
->queue_depth
= i
;
1177 pr_warn("%s: queue depth set to %u because of low memory\n",
1181 hctx
->tags
= blk_mq_init_tags(hctx
->queue_depth
, reserved_tags
, node
);
1184 blk_mq_free_rq_map(hctx
);
1191 static int blk_mq_init_hw_queues(struct request_queue
*q
,
1192 struct blk_mq_reg
*reg
, void *driver_data
)
1194 struct blk_mq_hw_ctx
*hctx
;
1198 * Initialize hardware queues
1200 queue_for_each_hw_ctx(q
, hctx
, i
) {
1201 unsigned int num_maps
;
1204 node
= hctx
->numa_node
;
1205 if (node
== NUMA_NO_NODE
)
1206 node
= hctx
->numa_node
= reg
->numa_node
;
1208 INIT_DELAYED_WORK(&hctx
->delayed_work
, blk_mq_work_fn
);
1209 spin_lock_init(&hctx
->lock
);
1210 INIT_LIST_HEAD(&hctx
->dispatch
);
1212 hctx
->queue_num
= i
;
1213 hctx
->flags
= reg
->flags
;
1214 hctx
->queue_depth
= reg
->queue_depth
;
1215 hctx
->cmd_size
= reg
->cmd_size
;
1217 blk_mq_init_cpu_notifier(&hctx
->cpu_notifier
,
1218 blk_mq_hctx_notify
, hctx
);
1219 blk_mq_register_cpu_notifier(&hctx
->cpu_notifier
);
1221 if (blk_mq_init_rq_map(hctx
, reg
->reserved_tags
, node
))
1225 * Allocate space for all possible cpus to avoid allocation in
1228 hctx
->ctxs
= kmalloc_node(nr_cpu_ids
* sizeof(void *),
1233 num_maps
= ALIGN(nr_cpu_ids
, BITS_PER_LONG
) / BITS_PER_LONG
;
1234 hctx
->ctx_map
= kzalloc_node(num_maps
* sizeof(unsigned long),
1239 hctx
->nr_ctx_map
= num_maps
;
1242 if (reg
->ops
->init_hctx
&&
1243 reg
->ops
->init_hctx(hctx
, driver_data
, i
))
1247 if (i
== q
->nr_hw_queues
)
1253 queue_for_each_hw_ctx(q
, hctx
, j
) {
1257 if (reg
->ops
->exit_hctx
)
1258 reg
->ops
->exit_hctx(hctx
, j
);
1260 blk_mq_unregister_cpu_notifier(&hctx
->cpu_notifier
);
1261 blk_mq_free_rq_map(hctx
);
1268 static void blk_mq_init_cpu_queues(struct request_queue
*q
,
1269 unsigned int nr_hw_queues
)
1273 for_each_possible_cpu(i
) {
1274 struct blk_mq_ctx
*__ctx
= per_cpu_ptr(q
->queue_ctx
, i
);
1275 struct blk_mq_hw_ctx
*hctx
;
1277 memset(__ctx
, 0, sizeof(*__ctx
));
1279 spin_lock_init(&__ctx
->lock
);
1280 INIT_LIST_HEAD(&__ctx
->rq_list
);
1283 /* If the cpu isn't online, the cpu is mapped to first hctx */
1284 hctx
= q
->mq_ops
->map_queue(q
, i
);
1291 * Set local node, IFF we have more than one hw queue. If
1292 * not, we remain on the home node of the device
1294 if (nr_hw_queues
> 1 && hctx
->numa_node
== NUMA_NO_NODE
)
1295 hctx
->numa_node
= cpu_to_node(i
);
1299 static void blk_mq_map_swqueue(struct request_queue
*q
)
1302 struct blk_mq_hw_ctx
*hctx
;
1303 struct blk_mq_ctx
*ctx
;
1305 queue_for_each_hw_ctx(q
, hctx
, i
) {
1310 * Map software to hardware queues
1312 queue_for_each_ctx(q
, ctx
, i
) {
1313 /* If the cpu isn't online, the cpu is mapped to first hctx */
1314 hctx
= q
->mq_ops
->map_queue(q
, i
);
1315 ctx
->index_hw
= hctx
->nr_ctx
;
1316 hctx
->ctxs
[hctx
->nr_ctx
++] = ctx
;
1320 struct request_queue
*blk_mq_init_queue(struct blk_mq_reg
*reg
,
1323 struct blk_mq_hw_ctx
**hctxs
;
1324 struct blk_mq_ctx
*ctx
;
1325 struct request_queue
*q
;
1328 if (!reg
->nr_hw_queues
||
1329 !reg
->ops
->queue_rq
|| !reg
->ops
->map_queue
||
1330 !reg
->ops
->alloc_hctx
|| !reg
->ops
->free_hctx
)
1331 return ERR_PTR(-EINVAL
);
1333 if (!reg
->queue_depth
)
1334 reg
->queue_depth
= BLK_MQ_MAX_DEPTH
;
1335 else if (reg
->queue_depth
> BLK_MQ_MAX_DEPTH
) {
1336 pr_err("blk-mq: queuedepth too large (%u)\n", reg
->queue_depth
);
1337 reg
->queue_depth
= BLK_MQ_MAX_DEPTH
;
1341 * Set aside a tag for flush requests. It will only be used while
1342 * another flush request is in progress but outside the driver.
1344 * TODO: only allocate if flushes are supported
1347 reg
->reserved_tags
++;
1349 if (reg
->queue_depth
< (reg
->reserved_tags
+ BLK_MQ_TAG_MIN
))
1350 return ERR_PTR(-EINVAL
);
1352 ctx
= alloc_percpu(struct blk_mq_ctx
);
1354 return ERR_PTR(-ENOMEM
);
1356 hctxs
= kmalloc_node(reg
->nr_hw_queues
* sizeof(*hctxs
), GFP_KERNEL
,
1362 for (i
= 0; i
< reg
->nr_hw_queues
; i
++) {
1363 hctxs
[i
] = reg
->ops
->alloc_hctx(reg
, i
);
1367 hctxs
[i
]->numa_node
= NUMA_NO_NODE
;
1368 hctxs
[i
]->queue_num
= i
;
1371 q
= blk_alloc_queue_node(GFP_KERNEL
, reg
->numa_node
);
1375 q
->mq_map
= blk_mq_make_queue_map(reg
);
1379 setup_timer(&q
->timeout
, blk_mq_rq_timer
, (unsigned long) q
);
1380 blk_queue_rq_timeout(q
, 30000);
1382 q
->nr_queues
= nr_cpu_ids
;
1383 q
->nr_hw_queues
= reg
->nr_hw_queues
;
1386 q
->queue_hw_ctx
= hctxs
;
1388 q
->mq_ops
= reg
->ops
;
1389 q
->queue_flags
|= QUEUE_FLAG_MQ_DEFAULT
;
1391 blk_queue_make_request(q
, blk_mq_make_request
);
1392 blk_queue_rq_timed_out(q
, reg
->ops
->timeout
);
1394 blk_queue_rq_timeout(q
, reg
->timeout
);
1396 blk_mq_init_flush(q
);
1397 blk_mq_init_cpu_queues(q
, reg
->nr_hw_queues
);
1399 if (blk_mq_init_hw_queues(q
, reg
, driver_data
))
1402 blk_mq_map_swqueue(q
);
1404 mutex_lock(&all_q_mutex
);
1405 list_add_tail(&q
->all_q_node
, &all_q_list
);
1406 mutex_unlock(&all_q_mutex
);
1412 blk_cleanup_queue(q
);
1414 for (i
= 0; i
< reg
->nr_hw_queues
; i
++) {
1417 reg
->ops
->free_hctx(hctxs
[i
], i
);
1422 return ERR_PTR(-ENOMEM
);
1424 EXPORT_SYMBOL(blk_mq_init_queue
);
1426 void blk_mq_free_queue(struct request_queue
*q
)
1428 struct blk_mq_hw_ctx
*hctx
;
1431 queue_for_each_hw_ctx(q
, hctx
, i
) {
1432 cancel_delayed_work_sync(&hctx
->delayed_work
);
1433 kfree(hctx
->ctx_map
);
1435 blk_mq_free_rq_map(hctx
);
1436 blk_mq_unregister_cpu_notifier(&hctx
->cpu_notifier
);
1437 if (q
->mq_ops
->exit_hctx
)
1438 q
->mq_ops
->exit_hctx(hctx
, i
);
1439 q
->mq_ops
->free_hctx(hctx
, i
);
1442 free_percpu(q
->queue_ctx
);
1443 kfree(q
->queue_hw_ctx
);
1446 q
->queue_ctx
= NULL
;
1447 q
->queue_hw_ctx
= NULL
;
1450 mutex_lock(&all_q_mutex
);
1451 list_del_init(&q
->all_q_node
);
1452 mutex_unlock(&all_q_mutex
);
1454 EXPORT_SYMBOL(blk_mq_free_queue
);
1456 /* Basically redo blk_mq_init_queue with queue frozen */
1457 static void blk_mq_queue_reinit(struct request_queue
*q
)
1459 blk_mq_freeze_queue(q
);
1461 blk_mq_update_queue_map(q
->mq_map
, q
->nr_hw_queues
);
1464 * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
1465 * we should change hctx numa_node according to new topology (this
1466 * involves free and re-allocate memory, worthy doing?)
1469 blk_mq_map_swqueue(q
);
1471 blk_mq_unfreeze_queue(q
);
1474 static int blk_mq_queue_reinit_notify(struct notifier_block
*nb
,
1475 unsigned long action
, void *hcpu
)
1477 struct request_queue
*q
;
1480 * Before new mapping is established, hotadded cpu might already start
1481 * handling requests. This doesn't break anything as we map offline
1482 * CPUs to first hardware queue. We will re-init queue below to get
1485 if (action
!= CPU_DEAD
&& action
!= CPU_DEAD_FROZEN
&&
1486 action
!= CPU_ONLINE
&& action
!= CPU_ONLINE_FROZEN
)
1489 mutex_lock(&all_q_mutex
);
1490 list_for_each_entry(q
, &all_q_list
, all_q_node
)
1491 blk_mq_queue_reinit(q
);
1492 mutex_unlock(&all_q_mutex
);
1496 static int __init
blk_mq_init(void)
1500 for_each_possible_cpu(i
)
1501 init_llist_head(&per_cpu(ipi_lists
, i
));
1505 /* Must be called after percpu_counter_hotcpu_callback() */
1506 hotcpu_notifier(blk_mq_queue_reinit_notify
, -10);
1510 subsys_initcall(blk_mq_init
);