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 static struct blk_mq_ctx
*__blk_mq_get_ctx(struct request_queue
*q
,
33 return per_cpu_ptr(q
->queue_ctx
, cpu
);
37 * This assumes per-cpu software queueing queues. They could be per-node
38 * as well, for instance. For now this is hardcoded as-is. Note that we don't
39 * care about preemption, since we know the ctx's are persistent. This does
40 * mean that we can't rely on ctx always matching the currently running CPU.
42 static struct blk_mq_ctx
*blk_mq_get_ctx(struct request_queue
*q
)
44 return __blk_mq_get_ctx(q
, get_cpu());
47 static void blk_mq_put_ctx(struct blk_mq_ctx
*ctx
)
53 * Check if any of the ctx's have pending work in this hardware queue
55 static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx
*hctx
)
59 for (i
= 0; i
< hctx
->nr_ctx_map
; i
++)
67 * Mark this ctx as having pending work in this hardware queue
69 static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx
*hctx
,
70 struct blk_mq_ctx
*ctx
)
72 if (!test_bit(ctx
->index_hw
, hctx
->ctx_map
))
73 set_bit(ctx
->index_hw
, hctx
->ctx_map
);
76 static struct request
*__blk_mq_alloc_request(struct blk_mq_hw_ctx
*hctx
,
77 gfp_t gfp
, bool reserved
)
82 tag
= blk_mq_get_tag(hctx
->tags
, gfp
, reserved
);
83 if (tag
!= BLK_MQ_TAG_FAIL
) {
93 static int blk_mq_queue_enter(struct request_queue
*q
)
97 __percpu_counter_add(&q
->mq_usage_counter
, 1, 1000000);
99 /* we have problems to freeze the queue if it's initializing */
100 if (!blk_queue_bypass(q
) || !blk_queue_init_done(q
))
103 __percpu_counter_add(&q
->mq_usage_counter
, -1, 1000000);
105 spin_lock_irq(q
->queue_lock
);
106 ret
= wait_event_interruptible_lock_irq(q
->mq_freeze_wq
,
107 !blk_queue_bypass(q
) || blk_queue_dying(q
),
109 /* inc usage with lock hold to avoid freeze_queue runs here */
110 if (!ret
&& !blk_queue_dying(q
))
111 __percpu_counter_add(&q
->mq_usage_counter
, 1, 1000000);
112 else if (blk_queue_dying(q
))
114 spin_unlock_irq(q
->queue_lock
);
119 static void blk_mq_queue_exit(struct request_queue
*q
)
121 __percpu_counter_add(&q
->mq_usage_counter
, -1, 1000000);
124 static void __blk_mq_drain_queue(struct request_queue
*q
)
129 spin_lock_irq(q
->queue_lock
);
130 count
= percpu_counter_sum(&q
->mq_usage_counter
);
131 spin_unlock_irq(q
->queue_lock
);
135 blk_mq_run_queues(q
, false);
141 * Guarantee no request is in use, so we can change any data structure of
142 * the queue afterward.
144 static void blk_mq_freeze_queue(struct request_queue
*q
)
148 spin_lock_irq(q
->queue_lock
);
149 drain
= !q
->bypass_depth
++;
150 queue_flag_set(QUEUE_FLAG_BYPASS
, q
);
151 spin_unlock_irq(q
->queue_lock
);
154 __blk_mq_drain_queue(q
);
157 void blk_mq_drain_queue(struct request_queue
*q
)
159 __blk_mq_drain_queue(q
);
162 static void blk_mq_unfreeze_queue(struct request_queue
*q
)
166 spin_lock_irq(q
->queue_lock
);
167 if (!--q
->bypass_depth
) {
168 queue_flag_clear(QUEUE_FLAG_BYPASS
, q
);
171 WARN_ON_ONCE(q
->bypass_depth
< 0);
172 spin_unlock_irq(q
->queue_lock
);
174 wake_up_all(&q
->mq_freeze_wq
);
177 bool blk_mq_can_queue(struct blk_mq_hw_ctx
*hctx
)
179 return blk_mq_has_free_tags(hctx
->tags
);
181 EXPORT_SYMBOL(blk_mq_can_queue
);
183 static void blk_mq_rq_ctx_init(struct request_queue
*q
, struct blk_mq_ctx
*ctx
,
184 struct request
*rq
, unsigned int rw_flags
)
186 if (blk_queue_io_stat(q
))
187 rw_flags
|= REQ_IO_STAT
;
190 rq
->cmd_flags
= rw_flags
;
191 rq
->start_time
= jiffies
;
192 set_start_time_ns(rq
);
193 ctx
->rq_dispatched
[rw_is_sync(rw_flags
)]++;
196 static struct request
*blk_mq_alloc_request_pinned(struct request_queue
*q
,
203 struct blk_mq_ctx
*ctx
= blk_mq_get_ctx(q
);
204 struct blk_mq_hw_ctx
*hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
206 rq
= __blk_mq_alloc_request(hctx
, gfp
& ~__GFP_WAIT
, reserved
);
208 blk_mq_rq_ctx_init(q
, ctx
, rq
, rw
);
212 if (gfp
& __GFP_WAIT
) {
213 __blk_mq_run_hw_queue(hctx
);
220 blk_mq_wait_for_tags(hctx
->tags
);
226 struct request
*blk_mq_alloc_request(struct request_queue
*q
, int rw
, gfp_t gfp
)
230 if (blk_mq_queue_enter(q
))
233 rq
= blk_mq_alloc_request_pinned(q
, rw
, gfp
, false);
235 blk_mq_put_ctx(rq
->mq_ctx
);
239 struct request
*blk_mq_alloc_reserved_request(struct request_queue
*q
, int rw
,
244 if (blk_mq_queue_enter(q
))
247 rq
= blk_mq_alloc_request_pinned(q
, rw
, gfp
, true);
249 blk_mq_put_ctx(rq
->mq_ctx
);
252 EXPORT_SYMBOL(blk_mq_alloc_reserved_request
);
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_rq_init(hctx
->queue
, 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
);
279 * Clone all relevant state from a request that has been put on hold in
280 * the flush state machine into the preallocated flush request that hangs
281 * off the request queue.
283 * For a driver the flush request should be invisible, that's why we are
284 * impersonating the original request here.
286 void blk_mq_clone_flush_request(struct request
*flush_rq
,
287 struct request
*orig_rq
)
289 struct blk_mq_hw_ctx
*hctx
=
290 orig_rq
->q
->mq_ops
->map_queue(orig_rq
->q
, orig_rq
->mq_ctx
->cpu
);
292 flush_rq
->mq_ctx
= orig_rq
->mq_ctx
;
293 flush_rq
->tag
= orig_rq
->tag
;
294 memcpy(blk_mq_rq_to_pdu(flush_rq
), blk_mq_rq_to_pdu(orig_rq
),
298 bool blk_mq_end_io_partial(struct request
*rq
, int error
, unsigned int nr_bytes
)
300 if (blk_update_request(rq
, error
, blk_rq_bytes(rq
)))
303 blk_account_io_done(rq
);
306 rq
->end_io(rq
, error
);
308 blk_mq_free_request(rq
);
311 EXPORT_SYMBOL(blk_mq_end_io_partial
);
313 static void __blk_mq_complete_request_remote(void *data
)
315 struct request
*rq
= data
;
317 rq
->q
->softirq_done_fn(rq
);
320 void __blk_mq_complete_request(struct request
*rq
)
322 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
;
325 if (!ctx
->ipi_redirect
) {
326 rq
->q
->softirq_done_fn(rq
);
331 if (cpu
!= ctx
->cpu
&& cpu_online(ctx
->cpu
)) {
332 rq
->csd
.func
= __blk_mq_complete_request_remote
;
335 smp_call_function_single_async(ctx
->cpu
, &rq
->csd
);
337 rq
->q
->softirq_done_fn(rq
);
343 * blk_mq_complete_request - end I/O on a request
344 * @rq: the request being processed
347 * Ends all I/O on a request. It does not handle partial completions.
348 * The actual completion happens out-of-order, through a IPI handler.
350 void blk_mq_complete_request(struct request
*rq
)
352 if (unlikely(blk_should_fake_timeout(rq
->q
)))
354 if (!blk_mark_rq_complete(rq
))
355 __blk_mq_complete_request(rq
);
357 EXPORT_SYMBOL(blk_mq_complete_request
);
359 static void blk_mq_start_request(struct request
*rq
, bool last
)
361 struct request_queue
*q
= rq
->q
;
363 trace_block_rq_issue(q
, rq
);
365 rq
->resid_len
= blk_rq_bytes(rq
);
368 * Just mark start time and set the started bit. Due to memory
369 * ordering, we know we'll see the correct deadline as long as
370 * REQ_ATOMIC_STARTED is seen.
372 rq
->deadline
= jiffies
+ q
->rq_timeout
;
373 set_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
);
375 if (q
->dma_drain_size
&& blk_rq_bytes(rq
)) {
377 * Make sure space for the drain appears. We know we can do
378 * this because max_hw_segments has been adjusted to be one
379 * fewer than the device can handle.
381 rq
->nr_phys_segments
++;
385 * Flag the last request in the series so that drivers know when IO
386 * should be kicked off, if they don't do it on a per-request basis.
388 * Note: the flag isn't the only condition drivers should do kick off.
389 * If drive is busy, the last request might not have the bit set.
392 rq
->cmd_flags
|= REQ_END
;
395 static void blk_mq_requeue_request(struct request
*rq
)
397 struct request_queue
*q
= rq
->q
;
399 trace_block_rq_requeue(q
, rq
);
400 clear_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
);
402 rq
->cmd_flags
&= ~REQ_END
;
404 if (q
->dma_drain_size
&& blk_rq_bytes(rq
))
405 rq
->nr_phys_segments
--;
408 struct blk_mq_timeout_data
{
409 struct blk_mq_hw_ctx
*hctx
;
411 unsigned int *next_set
;
414 static void blk_mq_timeout_check(void *__data
, unsigned long *free_tags
)
416 struct blk_mq_timeout_data
*data
= __data
;
417 struct blk_mq_hw_ctx
*hctx
= data
->hctx
;
420 /* It may not be in flight yet (this is where
421 * the REQ_ATOMIC_STARTED flag comes in). The requests are
422 * statically allocated, so we know it's always safe to access the
423 * memory associated with a bit offset into ->rqs[].
429 tag
= find_next_zero_bit(free_tags
, hctx
->queue_depth
, tag
);
430 if (tag
>= hctx
->queue_depth
)
433 rq
= hctx
->rqs
[tag
++];
435 if (!test_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
))
438 blk_rq_check_expired(rq
, data
->next
, data
->next_set
);
442 static void blk_mq_hw_ctx_check_timeout(struct blk_mq_hw_ctx
*hctx
,
444 unsigned int *next_set
)
446 struct blk_mq_timeout_data data
= {
449 .next_set
= next_set
,
453 * Ask the tagging code to iterate busy requests, so we can
454 * check them for timeout.
456 blk_mq_tag_busy_iter(hctx
->tags
, blk_mq_timeout_check
, &data
);
459 static void blk_mq_rq_timer(unsigned long data
)
461 struct request_queue
*q
= (struct request_queue
*) data
;
462 struct blk_mq_hw_ctx
*hctx
;
463 unsigned long next
= 0;
466 queue_for_each_hw_ctx(q
, hctx
, i
)
467 blk_mq_hw_ctx_check_timeout(hctx
, &next
, &next_set
);
470 mod_timer(&q
->timeout
, round_jiffies_up(next
));
474 * Reverse check our software queue for entries that we could potentially
475 * merge with. Currently includes a hand-wavy stop count of 8, to not spend
476 * too much time checking for merges.
478 static bool blk_mq_attempt_merge(struct request_queue
*q
,
479 struct blk_mq_ctx
*ctx
, struct bio
*bio
)
484 list_for_each_entry_reverse(rq
, &ctx
->rq_list
, queuelist
) {
490 if (!blk_rq_merge_ok(rq
, bio
))
493 el_ret
= blk_try_merge(rq
, bio
);
494 if (el_ret
== ELEVATOR_BACK_MERGE
) {
495 if (bio_attempt_back_merge(q
, rq
, bio
)) {
500 } else if (el_ret
== ELEVATOR_FRONT_MERGE
) {
501 if (bio_attempt_front_merge(q
, rq
, bio
)) {
512 void blk_mq_add_timer(struct request
*rq
)
514 __blk_add_timer(rq
, NULL
);
518 * Run this hardware queue, pulling any software queues mapped to it in.
519 * Note that this function currently has various problems around ordering
520 * of IO. In particular, we'd like FIFO behaviour on handling existing
521 * items on the hctx->dispatch list. Ignore that for now.
523 static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx
*hctx
)
525 struct request_queue
*q
= hctx
->queue
;
526 struct blk_mq_ctx
*ctx
;
531 WARN_ON(!preempt_count());
533 if (unlikely(test_bit(BLK_MQ_S_STOPPED
, &hctx
->state
)))
539 * Touch any software queue that has pending entries.
541 for_each_set_bit(bit
, hctx
->ctx_map
, hctx
->nr_ctx
) {
542 clear_bit(bit
, hctx
->ctx_map
);
543 ctx
= hctx
->ctxs
[bit
];
544 BUG_ON(bit
!= ctx
->index_hw
);
546 spin_lock(&ctx
->lock
);
547 list_splice_tail_init(&ctx
->rq_list
, &rq_list
);
548 spin_unlock(&ctx
->lock
);
552 * If we have previous entries on our dispatch list, grab them
553 * and stuff them at the front for more fair dispatch.
555 if (!list_empty_careful(&hctx
->dispatch
)) {
556 spin_lock(&hctx
->lock
);
557 if (!list_empty(&hctx
->dispatch
))
558 list_splice_init(&hctx
->dispatch
, &rq_list
);
559 spin_unlock(&hctx
->lock
);
563 * Delete and return all entries from our dispatch list
568 * Now process all the entries, sending them to the driver.
570 while (!list_empty(&rq_list
)) {
573 rq
= list_first_entry(&rq_list
, struct request
, queuelist
);
574 list_del_init(&rq
->queuelist
);
576 blk_mq_start_request(rq
, list_empty(&rq_list
));
578 ret
= q
->mq_ops
->queue_rq(hctx
, rq
);
580 case BLK_MQ_RQ_QUEUE_OK
:
583 case BLK_MQ_RQ_QUEUE_BUSY
:
585 * FIXME: we should have a mechanism to stop the queue
586 * like blk_stop_queue, otherwise we will waste cpu
589 list_add(&rq
->queuelist
, &rq_list
);
590 blk_mq_requeue_request(rq
);
593 pr_err("blk-mq: bad return on queue: %d\n", ret
);
594 case BLK_MQ_RQ_QUEUE_ERROR
:
596 blk_mq_end_io(rq
, rq
->errors
);
600 if (ret
== BLK_MQ_RQ_QUEUE_BUSY
)
605 hctx
->dispatched
[0]++;
606 else if (queued
< (1 << (BLK_MQ_MAX_DISPATCH_ORDER
- 1)))
607 hctx
->dispatched
[ilog2(queued
) + 1]++;
610 * Any items that need requeuing? Stuff them into hctx->dispatch,
611 * that is where we will continue on next queue run.
613 if (!list_empty(&rq_list
)) {
614 spin_lock(&hctx
->lock
);
615 list_splice(&rq_list
, &hctx
->dispatch
);
616 spin_unlock(&hctx
->lock
);
620 void blk_mq_run_hw_queue(struct blk_mq_hw_ctx
*hctx
, bool async
)
622 if (unlikely(test_bit(BLK_MQ_S_STOPPED
, &hctx
->state
)))
625 if (!async
&& cpumask_test_cpu(smp_processor_id(), hctx
->cpumask
))
626 __blk_mq_run_hw_queue(hctx
);
627 else if (hctx
->queue
->nr_hw_queues
== 1)
628 kblockd_schedule_delayed_work(&hctx
->delayed_work
, 0);
633 * It'd be great if the workqueue API had a way to pass
634 * in a mask and had some smarts for more clever placement
635 * than the first CPU. Or we could round-robin here. For now,
636 * just queue on the first CPU.
638 cpu
= cpumask_first(hctx
->cpumask
);
639 kblockd_schedule_delayed_work_on(cpu
, &hctx
->delayed_work
, 0);
643 void blk_mq_run_queues(struct request_queue
*q
, bool async
)
645 struct blk_mq_hw_ctx
*hctx
;
648 queue_for_each_hw_ctx(q
, hctx
, i
) {
649 if ((!blk_mq_hctx_has_pending(hctx
) &&
650 list_empty_careful(&hctx
->dispatch
)) ||
651 test_bit(BLK_MQ_S_STOPPED
, &hctx
->state
))
655 blk_mq_run_hw_queue(hctx
, async
);
659 EXPORT_SYMBOL(blk_mq_run_queues
);
661 void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx
*hctx
)
663 cancel_delayed_work(&hctx
->delayed_work
);
664 set_bit(BLK_MQ_S_STOPPED
, &hctx
->state
);
666 EXPORT_SYMBOL(blk_mq_stop_hw_queue
);
668 void blk_mq_stop_hw_queues(struct request_queue
*q
)
670 struct blk_mq_hw_ctx
*hctx
;
673 queue_for_each_hw_ctx(q
, hctx
, i
)
674 blk_mq_stop_hw_queue(hctx
);
676 EXPORT_SYMBOL(blk_mq_stop_hw_queues
);
678 void blk_mq_start_hw_queue(struct blk_mq_hw_ctx
*hctx
)
680 clear_bit(BLK_MQ_S_STOPPED
, &hctx
->state
);
683 __blk_mq_run_hw_queue(hctx
);
686 EXPORT_SYMBOL(blk_mq_start_hw_queue
);
688 void blk_mq_start_stopped_hw_queues(struct request_queue
*q
)
690 struct blk_mq_hw_ctx
*hctx
;
693 queue_for_each_hw_ctx(q
, hctx
, i
) {
694 if (!test_bit(BLK_MQ_S_STOPPED
, &hctx
->state
))
697 clear_bit(BLK_MQ_S_STOPPED
, &hctx
->state
);
699 blk_mq_run_hw_queue(hctx
, true);
703 EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues
);
705 static void blk_mq_work_fn(struct work_struct
*work
)
707 struct blk_mq_hw_ctx
*hctx
;
709 hctx
= container_of(work
, struct blk_mq_hw_ctx
, delayed_work
.work
);
712 __blk_mq_run_hw_queue(hctx
);
716 static void __blk_mq_insert_request(struct blk_mq_hw_ctx
*hctx
,
717 struct request
*rq
, bool at_head
)
719 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
;
721 trace_block_rq_insert(hctx
->queue
, rq
);
724 list_add(&rq
->queuelist
, &ctx
->rq_list
);
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
*rq
, bool at_head
, bool run_queue
,
738 struct request_queue
*q
= rq
->q
;
739 struct blk_mq_hw_ctx
*hctx
;
740 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
, *current_ctx
;
742 current_ctx
= blk_mq_get_ctx(q
);
743 if (!cpu_online(ctx
->cpu
))
744 rq
->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 !(rq
->cmd_flags
& (REQ_FLUSH_SEQ
))) {
750 blk_insert_flush(rq
);
752 spin_lock(&ctx
->lock
);
753 __blk_mq_insert_request(hctx
, rq
, at_head
);
754 spin_unlock(&ctx
->lock
);
758 blk_mq_run_hw_queue(hctx
, async
);
760 blk_mq_put_ctx(current_ctx
);
763 static void blk_mq_insert_requests(struct request_queue
*q
,
764 struct blk_mq_ctx
*ctx
,
765 struct list_head
*list
,
770 struct blk_mq_hw_ctx
*hctx
;
771 struct blk_mq_ctx
*current_ctx
;
773 trace_block_unplug(q
, depth
, !from_schedule
);
775 current_ctx
= blk_mq_get_ctx(q
);
777 if (!cpu_online(ctx
->cpu
))
779 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
782 * preemption doesn't flush plug list, so it's possible ctx->cpu is
785 spin_lock(&ctx
->lock
);
786 while (!list_empty(list
)) {
789 rq
= list_first_entry(list
, struct request
, queuelist
);
790 list_del_init(&rq
->queuelist
);
792 __blk_mq_insert_request(hctx
, rq
, false);
794 spin_unlock(&ctx
->lock
);
796 blk_mq_run_hw_queue(hctx
, from_schedule
);
797 blk_mq_put_ctx(current_ctx
);
800 static int plug_ctx_cmp(void *priv
, struct list_head
*a
, struct list_head
*b
)
802 struct request
*rqa
= container_of(a
, struct request
, queuelist
);
803 struct request
*rqb
= container_of(b
, struct request
, queuelist
);
805 return !(rqa
->mq_ctx
< rqb
->mq_ctx
||
806 (rqa
->mq_ctx
== rqb
->mq_ctx
&&
807 blk_rq_pos(rqa
) < blk_rq_pos(rqb
)));
810 void blk_mq_flush_plug_list(struct blk_plug
*plug
, bool from_schedule
)
812 struct blk_mq_ctx
*this_ctx
;
813 struct request_queue
*this_q
;
819 list_splice_init(&plug
->mq_list
, &list
);
821 list_sort(NULL
, &list
, plug_ctx_cmp
);
827 while (!list_empty(&list
)) {
828 rq
= list_entry_rq(list
.next
);
829 list_del_init(&rq
->queuelist
);
831 if (rq
->mq_ctx
!= this_ctx
) {
833 blk_mq_insert_requests(this_q
, this_ctx
,
838 this_ctx
= rq
->mq_ctx
;
844 list_add_tail(&rq
->queuelist
, &ctx_list
);
848 * If 'this_ctx' is set, we know we have entries to complete
849 * on 'ctx_list'. Do those.
852 blk_mq_insert_requests(this_q
, this_ctx
, &ctx_list
, depth
,
857 static void blk_mq_bio_to_request(struct request
*rq
, struct bio
*bio
)
859 init_request_from_bio(rq
, bio
);
860 blk_account_io_start(rq
, 1);
863 static void blk_mq_make_request(struct request_queue
*q
, struct bio
*bio
)
865 struct blk_mq_hw_ctx
*hctx
;
866 struct blk_mq_ctx
*ctx
;
867 const int is_sync
= rw_is_sync(bio
->bi_rw
);
868 const int is_flush_fua
= bio
->bi_rw
& (REQ_FLUSH
| REQ_FUA
);
869 int rw
= bio_data_dir(bio
);
871 unsigned int use_plug
, request_count
= 0;
874 * If we have multiple hardware queues, just go directly to
875 * one of those for sync IO.
877 use_plug
= !is_flush_fua
&& ((q
->nr_hw_queues
== 1) || !is_sync
);
879 blk_queue_bounce(q
, &bio
);
881 if (bio_integrity_enabled(bio
) && bio_integrity_prep(bio
)) {
882 bio_endio(bio
, -EIO
);
886 if (use_plug
&& blk_attempt_plug_merge(q
, bio
, &request_count
))
889 if (blk_mq_queue_enter(q
)) {
890 bio_endio(bio
, -EIO
);
894 ctx
= blk_mq_get_ctx(q
);
895 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
899 trace_block_getrq(q
, bio
, rw
);
900 rq
= __blk_mq_alloc_request(hctx
, GFP_ATOMIC
, false);
902 blk_mq_rq_ctx_init(q
, ctx
, rq
, rw
);
905 trace_block_sleeprq(q
, bio
, rw
);
906 rq
= blk_mq_alloc_request_pinned(q
, rw
, __GFP_WAIT
|GFP_ATOMIC
,
909 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
914 if (unlikely(is_flush_fua
)) {
915 blk_mq_bio_to_request(rq
, bio
);
916 blk_insert_flush(rq
);
921 * A task plug currently exists. Since this is completely lockless,
922 * utilize that to temporarily store requests until the task is
923 * either done or scheduled away.
926 struct blk_plug
*plug
= current
->plug
;
929 blk_mq_bio_to_request(rq
, bio
);
930 if (list_empty(&plug
->mq_list
))
932 else if (request_count
>= BLK_MAX_REQUEST_COUNT
) {
933 blk_flush_plug_list(plug
, false);
936 list_add_tail(&rq
->queuelist
, &plug
->mq_list
);
942 spin_lock(&ctx
->lock
);
944 if ((hctx
->flags
& BLK_MQ_F_SHOULD_MERGE
) &&
945 blk_mq_attempt_merge(q
, ctx
, bio
))
946 __blk_mq_free_request(hctx
, ctx
, rq
);
948 blk_mq_bio_to_request(rq
, bio
);
949 __blk_mq_insert_request(hctx
, rq
, false);
952 spin_unlock(&ctx
->lock
);
955 * For a SYNC request, send it to the hardware immediately. For an
956 * ASYNC request, just ensure that we run it later on. The latter
957 * allows for merging opportunities and more efficient dispatching.
960 blk_mq_run_hw_queue(hctx
, !is_sync
|| is_flush_fua
);
965 * Default mapping to a software queue, since we use one per CPU.
967 struct blk_mq_hw_ctx
*blk_mq_map_queue(struct request_queue
*q
, const int cpu
)
969 return q
->queue_hw_ctx
[q
->mq_map
[cpu
]];
971 EXPORT_SYMBOL(blk_mq_map_queue
);
973 struct blk_mq_hw_ctx
*blk_mq_alloc_single_hw_queue(struct blk_mq_reg
*reg
,
974 unsigned int hctx_index
)
976 return kmalloc_node(sizeof(struct blk_mq_hw_ctx
),
977 GFP_KERNEL
| __GFP_ZERO
, reg
->numa_node
);
979 EXPORT_SYMBOL(blk_mq_alloc_single_hw_queue
);
981 void blk_mq_free_single_hw_queue(struct blk_mq_hw_ctx
*hctx
,
982 unsigned int hctx_index
)
986 EXPORT_SYMBOL(blk_mq_free_single_hw_queue
);
988 static void blk_mq_hctx_notify(void *data
, unsigned long action
,
991 struct blk_mq_hw_ctx
*hctx
= data
;
992 struct request_queue
*q
= hctx
->queue
;
993 struct blk_mq_ctx
*ctx
;
996 if (action
!= CPU_DEAD
&& action
!= CPU_DEAD_FROZEN
)
1000 * Move ctx entries to new CPU, if this one is going away.
1002 ctx
= __blk_mq_get_ctx(q
, cpu
);
1004 spin_lock(&ctx
->lock
);
1005 if (!list_empty(&ctx
->rq_list
)) {
1006 list_splice_init(&ctx
->rq_list
, &tmp
);
1007 clear_bit(ctx
->index_hw
, hctx
->ctx_map
);
1009 spin_unlock(&ctx
->lock
);
1011 if (list_empty(&tmp
))
1014 ctx
= blk_mq_get_ctx(q
);
1015 spin_lock(&ctx
->lock
);
1017 while (!list_empty(&tmp
)) {
1020 rq
= list_first_entry(&tmp
, struct request
, queuelist
);
1022 list_move_tail(&rq
->queuelist
, &ctx
->rq_list
);
1025 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
1026 blk_mq_hctx_mark_pending(hctx
, ctx
);
1028 spin_unlock(&ctx
->lock
);
1030 blk_mq_run_hw_queue(hctx
, true);
1031 blk_mq_put_ctx(ctx
);
1034 static int blk_mq_init_hw_commands(struct blk_mq_hw_ctx
*hctx
,
1035 int (*init
)(void *, struct blk_mq_hw_ctx
*,
1036 struct request
*, unsigned int),
1042 for (i
= 0; i
< hctx
->queue_depth
; i
++) {
1043 struct request
*rq
= hctx
->rqs
[i
];
1045 ret
= init(data
, hctx
, rq
, i
);
1053 int blk_mq_init_commands(struct request_queue
*q
,
1054 int (*init
)(void *, struct blk_mq_hw_ctx
*,
1055 struct request
*, unsigned int),
1058 struct blk_mq_hw_ctx
*hctx
;
1062 queue_for_each_hw_ctx(q
, hctx
, i
) {
1063 ret
= blk_mq_init_hw_commands(hctx
, init
, data
);
1070 EXPORT_SYMBOL(blk_mq_init_commands
);
1072 static void blk_mq_free_hw_commands(struct blk_mq_hw_ctx
*hctx
,
1073 void (*free
)(void *, struct blk_mq_hw_ctx
*,
1074 struct request
*, unsigned int),
1079 for (i
= 0; i
< hctx
->queue_depth
; i
++) {
1080 struct request
*rq
= hctx
->rqs
[i
];
1082 free(data
, hctx
, rq
, i
);
1086 void blk_mq_free_commands(struct request_queue
*q
,
1087 void (*free
)(void *, struct blk_mq_hw_ctx
*,
1088 struct request
*, unsigned int),
1091 struct blk_mq_hw_ctx
*hctx
;
1094 queue_for_each_hw_ctx(q
, hctx
, i
)
1095 blk_mq_free_hw_commands(hctx
, free
, data
);
1097 EXPORT_SYMBOL(blk_mq_free_commands
);
1099 static void blk_mq_free_rq_map(struct blk_mq_hw_ctx
*hctx
)
1103 while (!list_empty(&hctx
->page_list
)) {
1104 page
= list_first_entry(&hctx
->page_list
, struct page
, lru
);
1105 list_del_init(&page
->lru
);
1106 __free_pages(page
, page
->private);
1112 blk_mq_free_tags(hctx
->tags
);
1115 static size_t order_to_size(unsigned int order
)
1117 size_t ret
= PAGE_SIZE
;
1125 static int blk_mq_init_rq_map(struct blk_mq_hw_ctx
*hctx
,
1126 unsigned int reserved_tags
, int node
)
1128 unsigned int i
, j
, entries_per_page
, max_order
= 4;
1129 size_t rq_size
, left
;
1131 INIT_LIST_HEAD(&hctx
->page_list
);
1133 hctx
->rqs
= kmalloc_node(hctx
->queue_depth
* sizeof(struct request
*),
1139 * rq_size is the size of the request plus driver payload, rounded
1140 * to the cacheline size
1142 rq_size
= round_up(sizeof(struct request
) + hctx
->cmd_size
,
1144 left
= rq_size
* hctx
->queue_depth
;
1146 for (i
= 0; i
< hctx
->queue_depth
;) {
1147 int this_order
= max_order
;
1152 while (left
< order_to_size(this_order
- 1) && this_order
)
1156 page
= alloc_pages_node(node
, GFP_KERNEL
, this_order
);
1161 if (order_to_size(this_order
) < rq_size
)
1168 page
->private = this_order
;
1169 list_add_tail(&page
->lru
, &hctx
->page_list
);
1171 p
= page_address(page
);
1172 entries_per_page
= order_to_size(this_order
) / rq_size
;
1173 to_do
= min(entries_per_page
, hctx
->queue_depth
- i
);
1174 left
-= to_do
* rq_size
;
1175 for (j
= 0; j
< to_do
; j
++) {
1177 blk_rq_init(hctx
->queue
, hctx
->rqs
[i
]);
1183 if (i
< (reserved_tags
+ BLK_MQ_TAG_MIN
))
1185 else if (i
!= hctx
->queue_depth
) {
1186 hctx
->queue_depth
= i
;
1187 pr_warn("%s: queue depth set to %u because of low memory\n",
1191 hctx
->tags
= blk_mq_init_tags(hctx
->queue_depth
, reserved_tags
, node
);
1194 blk_mq_free_rq_map(hctx
);
1201 static int blk_mq_init_hw_queues(struct request_queue
*q
,
1202 struct blk_mq_reg
*reg
, void *driver_data
)
1204 struct blk_mq_hw_ctx
*hctx
;
1208 * Initialize hardware queues
1210 queue_for_each_hw_ctx(q
, hctx
, i
) {
1211 unsigned int num_maps
;
1214 node
= hctx
->numa_node
;
1215 if (node
== NUMA_NO_NODE
)
1216 node
= hctx
->numa_node
= reg
->numa_node
;
1218 INIT_DELAYED_WORK(&hctx
->delayed_work
, blk_mq_work_fn
);
1219 spin_lock_init(&hctx
->lock
);
1220 INIT_LIST_HEAD(&hctx
->dispatch
);
1222 hctx
->queue_num
= i
;
1223 hctx
->flags
= reg
->flags
;
1224 hctx
->queue_depth
= reg
->queue_depth
;
1225 hctx
->cmd_size
= reg
->cmd_size
;
1227 blk_mq_init_cpu_notifier(&hctx
->cpu_notifier
,
1228 blk_mq_hctx_notify
, hctx
);
1229 blk_mq_register_cpu_notifier(&hctx
->cpu_notifier
);
1231 if (blk_mq_init_rq_map(hctx
, reg
->reserved_tags
, node
))
1235 * Allocate space for all possible cpus to avoid allocation in
1238 hctx
->ctxs
= kmalloc_node(nr_cpu_ids
* sizeof(void *),
1243 num_maps
= ALIGN(nr_cpu_ids
, BITS_PER_LONG
) / BITS_PER_LONG
;
1244 hctx
->ctx_map
= kzalloc_node(num_maps
* sizeof(unsigned long),
1249 hctx
->nr_ctx_map
= num_maps
;
1252 if (reg
->ops
->init_hctx
&&
1253 reg
->ops
->init_hctx(hctx
, driver_data
, i
))
1257 if (i
== q
->nr_hw_queues
)
1263 queue_for_each_hw_ctx(q
, hctx
, j
) {
1267 if (reg
->ops
->exit_hctx
)
1268 reg
->ops
->exit_hctx(hctx
, j
);
1270 blk_mq_unregister_cpu_notifier(&hctx
->cpu_notifier
);
1271 blk_mq_free_rq_map(hctx
);
1278 static void blk_mq_init_cpu_queues(struct request_queue
*q
,
1279 unsigned int nr_hw_queues
)
1283 for_each_possible_cpu(i
) {
1284 struct blk_mq_ctx
*__ctx
= per_cpu_ptr(q
->queue_ctx
, i
);
1285 struct blk_mq_hw_ctx
*hctx
;
1287 memset(__ctx
, 0, sizeof(*__ctx
));
1289 spin_lock_init(&__ctx
->lock
);
1290 INIT_LIST_HEAD(&__ctx
->rq_list
);
1293 /* If the cpu isn't online, the cpu is mapped to first hctx */
1297 hctx
= q
->mq_ops
->map_queue(q
, i
);
1298 cpumask_set_cpu(i
, hctx
->cpumask
);
1302 * Set local node, IFF we have more than one hw queue. If
1303 * not, we remain on the home node of the device
1305 if (nr_hw_queues
> 1 && hctx
->numa_node
== NUMA_NO_NODE
)
1306 hctx
->numa_node
= cpu_to_node(i
);
1310 static void blk_mq_map_swqueue(struct request_queue
*q
)
1313 struct blk_mq_hw_ctx
*hctx
;
1314 struct blk_mq_ctx
*ctx
;
1316 queue_for_each_hw_ctx(q
, hctx
, i
) {
1317 cpumask_clear(hctx
->cpumask
);
1322 * Map software to hardware queues
1324 queue_for_each_ctx(q
, ctx
, i
) {
1325 /* If the cpu isn't online, the cpu is mapped to first hctx */
1329 hctx
= q
->mq_ops
->map_queue(q
, i
);
1330 cpumask_set_cpu(i
, hctx
->cpumask
);
1331 ctx
->index_hw
= hctx
->nr_ctx
;
1332 hctx
->ctxs
[hctx
->nr_ctx
++] = ctx
;
1336 struct request_queue
*blk_mq_init_queue(struct blk_mq_reg
*reg
,
1339 struct blk_mq_hw_ctx
**hctxs
;
1340 struct blk_mq_ctx
*ctx
;
1341 struct request_queue
*q
;
1344 if (!reg
->nr_hw_queues
||
1345 !reg
->ops
->queue_rq
|| !reg
->ops
->map_queue
||
1346 !reg
->ops
->alloc_hctx
|| !reg
->ops
->free_hctx
)
1347 return ERR_PTR(-EINVAL
);
1349 if (!reg
->queue_depth
)
1350 reg
->queue_depth
= BLK_MQ_MAX_DEPTH
;
1351 else if (reg
->queue_depth
> BLK_MQ_MAX_DEPTH
) {
1352 pr_err("blk-mq: queuedepth too large (%u)\n", reg
->queue_depth
);
1353 reg
->queue_depth
= BLK_MQ_MAX_DEPTH
;
1356 if (reg
->queue_depth
< (reg
->reserved_tags
+ BLK_MQ_TAG_MIN
))
1357 return ERR_PTR(-EINVAL
);
1359 ctx
= alloc_percpu(struct blk_mq_ctx
);
1361 return ERR_PTR(-ENOMEM
);
1363 hctxs
= kmalloc_node(reg
->nr_hw_queues
* sizeof(*hctxs
), GFP_KERNEL
,
1369 for (i
= 0; i
< reg
->nr_hw_queues
; i
++) {
1370 hctxs
[i
] = reg
->ops
->alloc_hctx(reg
, i
);
1374 if (!zalloc_cpumask_var(&hctxs
[i
]->cpumask
, GFP_KERNEL
))
1377 hctxs
[i
]->numa_node
= NUMA_NO_NODE
;
1378 hctxs
[i
]->queue_num
= i
;
1381 q
= blk_alloc_queue_node(GFP_KERNEL
, reg
->numa_node
);
1385 q
->mq_map
= blk_mq_make_queue_map(reg
);
1389 setup_timer(&q
->timeout
, blk_mq_rq_timer
, (unsigned long) q
);
1390 blk_queue_rq_timeout(q
, 30000);
1392 q
->nr_queues
= nr_cpu_ids
;
1393 q
->nr_hw_queues
= reg
->nr_hw_queues
;
1396 q
->queue_hw_ctx
= hctxs
;
1398 q
->mq_ops
= reg
->ops
;
1399 q
->queue_flags
|= QUEUE_FLAG_MQ_DEFAULT
;
1401 q
->sg_reserved_size
= INT_MAX
;
1403 blk_queue_make_request(q
, blk_mq_make_request
);
1404 blk_queue_rq_timed_out(q
, reg
->ops
->timeout
);
1406 blk_queue_rq_timeout(q
, reg
->timeout
);
1408 if (reg
->ops
->complete
)
1409 blk_queue_softirq_done(q
, reg
->ops
->complete
);
1411 blk_mq_init_flush(q
);
1412 blk_mq_init_cpu_queues(q
, reg
->nr_hw_queues
);
1414 q
->flush_rq
= kzalloc(round_up(sizeof(struct request
) + reg
->cmd_size
,
1415 cache_line_size()), GFP_KERNEL
);
1419 if (blk_mq_init_hw_queues(q
, reg
, driver_data
))
1422 blk_mq_map_swqueue(q
);
1424 mutex_lock(&all_q_mutex
);
1425 list_add_tail(&q
->all_q_node
, &all_q_list
);
1426 mutex_unlock(&all_q_mutex
);
1435 blk_cleanup_queue(q
);
1437 for (i
= 0; i
< reg
->nr_hw_queues
; i
++) {
1440 free_cpumask_var(hctxs
[i
]->cpumask
);
1441 reg
->ops
->free_hctx(hctxs
[i
], i
);
1446 return ERR_PTR(-ENOMEM
);
1448 EXPORT_SYMBOL(blk_mq_init_queue
);
1450 void blk_mq_free_queue(struct request_queue
*q
)
1452 struct blk_mq_hw_ctx
*hctx
;
1455 queue_for_each_hw_ctx(q
, hctx
, i
) {
1456 kfree(hctx
->ctx_map
);
1458 blk_mq_free_rq_map(hctx
);
1459 blk_mq_unregister_cpu_notifier(&hctx
->cpu_notifier
);
1460 if (q
->mq_ops
->exit_hctx
)
1461 q
->mq_ops
->exit_hctx(hctx
, i
);
1462 free_cpumask_var(hctx
->cpumask
);
1463 q
->mq_ops
->free_hctx(hctx
, i
);
1466 free_percpu(q
->queue_ctx
);
1467 kfree(q
->queue_hw_ctx
);
1470 q
->queue_ctx
= NULL
;
1471 q
->queue_hw_ctx
= NULL
;
1474 mutex_lock(&all_q_mutex
);
1475 list_del_init(&q
->all_q_node
);
1476 mutex_unlock(&all_q_mutex
);
1479 /* Basically redo blk_mq_init_queue with queue frozen */
1480 static void blk_mq_queue_reinit(struct request_queue
*q
)
1482 blk_mq_freeze_queue(q
);
1484 blk_mq_update_queue_map(q
->mq_map
, q
->nr_hw_queues
);
1487 * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
1488 * we should change hctx numa_node according to new topology (this
1489 * involves free and re-allocate memory, worthy doing?)
1492 blk_mq_map_swqueue(q
);
1494 blk_mq_unfreeze_queue(q
);
1497 static int blk_mq_queue_reinit_notify(struct notifier_block
*nb
,
1498 unsigned long action
, void *hcpu
)
1500 struct request_queue
*q
;
1503 * Before new mapping is established, hotadded cpu might already start
1504 * handling requests. This doesn't break anything as we map offline
1505 * CPUs to first hardware queue. We will re-init queue below to get
1508 if (action
!= CPU_DEAD
&& action
!= CPU_DEAD_FROZEN
&&
1509 action
!= CPU_ONLINE
&& action
!= CPU_ONLINE_FROZEN
)
1512 mutex_lock(&all_q_mutex
);
1513 list_for_each_entry(q
, &all_q_list
, all_q_node
)
1514 blk_mq_queue_reinit(q
);
1515 mutex_unlock(&all_q_mutex
);
1519 void blk_mq_disable_hotplug(void)
1521 mutex_lock(&all_q_mutex
);
1524 void blk_mq_enable_hotplug(void)
1526 mutex_unlock(&all_q_mutex
);
1529 static int __init
blk_mq_init(void)
1533 /* Must be called after percpu_counter_hotcpu_callback() */
1534 hotcpu_notifier(blk_mq_queue_reinit_notify
, -10);
1538 subsys_initcall(blk_mq_init
);