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
);
255 * Re-init and set pdu, if we have it
257 void blk_mq_rq_init(struct blk_mq_hw_ctx
*hctx
, struct request
*rq
)
259 blk_rq_init(hctx
->queue
, rq
);
262 rq
->special
= blk_mq_rq_to_pdu(rq
);
265 static void __blk_mq_free_request(struct blk_mq_hw_ctx
*hctx
,
266 struct blk_mq_ctx
*ctx
, struct request
*rq
)
268 const int tag
= rq
->tag
;
269 struct request_queue
*q
= rq
->q
;
271 blk_mq_rq_init(hctx
, rq
);
272 blk_mq_put_tag(hctx
->tags
, tag
);
274 blk_mq_queue_exit(q
);
277 void blk_mq_free_request(struct request
*rq
)
279 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
;
280 struct blk_mq_hw_ctx
*hctx
;
281 struct request_queue
*q
= rq
->q
;
283 ctx
->rq_completed
[rq_is_sync(rq
)]++;
285 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
286 __blk_mq_free_request(hctx
, ctx
, rq
);
289 bool blk_mq_end_io_partial(struct request
*rq
, int error
, unsigned int nr_bytes
)
291 if (blk_update_request(rq
, error
, blk_rq_bytes(rq
)))
294 blk_account_io_done(rq
);
297 rq
->end_io(rq
, error
);
299 blk_mq_free_request(rq
);
302 EXPORT_SYMBOL(blk_mq_end_io_partial
);
304 static void __blk_mq_complete_request_remote(void *data
)
306 struct request
*rq
= data
;
308 rq
->q
->softirq_done_fn(rq
);
311 void __blk_mq_complete_request(struct request
*rq
)
313 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
;
316 if (!ctx
->ipi_redirect
) {
317 rq
->q
->softirq_done_fn(rq
);
322 if (cpu
!= ctx
->cpu
&& cpu_online(ctx
->cpu
)) {
323 rq
->csd
.func
= __blk_mq_complete_request_remote
;
326 smp_call_function_single_async(ctx
->cpu
, &rq
->csd
);
328 rq
->q
->softirq_done_fn(rq
);
334 * blk_mq_complete_request - end I/O on a request
335 * @rq: the request being processed
338 * Ends all I/O on a request. It does not handle partial completions.
339 * The actual completion happens out-of-order, through a IPI handler.
341 void blk_mq_complete_request(struct request
*rq
)
343 if (unlikely(blk_should_fake_timeout(rq
->q
)))
345 if (!blk_mark_rq_complete(rq
))
346 __blk_mq_complete_request(rq
);
348 EXPORT_SYMBOL(blk_mq_complete_request
);
350 static void blk_mq_start_request(struct request
*rq
, bool last
)
352 struct request_queue
*q
= rq
->q
;
354 trace_block_rq_issue(q
, rq
);
356 rq
->resid_len
= blk_rq_bytes(rq
);
359 * Just mark start time and set the started bit. Due to memory
360 * ordering, we know we'll see the correct deadline as long as
361 * REQ_ATOMIC_STARTED is seen.
363 rq
->deadline
= jiffies
+ q
->rq_timeout
;
364 set_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
);
366 if (q
->dma_drain_size
&& blk_rq_bytes(rq
)) {
368 * Make sure space for the drain appears. We know we can do
369 * this because max_hw_segments has been adjusted to be one
370 * fewer than the device can handle.
372 rq
->nr_phys_segments
++;
376 * Flag the last request in the series so that drivers know when IO
377 * should be kicked off, if they don't do it on a per-request basis.
379 * Note: the flag isn't the only condition drivers should do kick off.
380 * If drive is busy, the last request might not have the bit set.
383 rq
->cmd_flags
|= REQ_END
;
386 static void blk_mq_requeue_request(struct request
*rq
)
388 struct request_queue
*q
= rq
->q
;
390 trace_block_rq_requeue(q
, rq
);
391 clear_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
);
393 rq
->cmd_flags
&= ~REQ_END
;
395 if (q
->dma_drain_size
&& blk_rq_bytes(rq
))
396 rq
->nr_phys_segments
--;
399 struct blk_mq_timeout_data
{
400 struct blk_mq_hw_ctx
*hctx
;
402 unsigned int *next_set
;
405 static void blk_mq_timeout_check(void *__data
, unsigned long *free_tags
)
407 struct blk_mq_timeout_data
*data
= __data
;
408 struct blk_mq_hw_ctx
*hctx
= data
->hctx
;
411 /* It may not be in flight yet (this is where
412 * the REQ_ATOMIC_STARTED flag comes in). The requests are
413 * statically allocated, so we know it's always safe to access the
414 * memory associated with a bit offset into ->rqs[].
420 tag
= find_next_zero_bit(free_tags
, hctx
->queue_depth
, tag
);
421 if (tag
>= hctx
->queue_depth
)
424 rq
= hctx
->rqs
[tag
++];
426 if (!test_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
))
429 blk_rq_check_expired(rq
, data
->next
, data
->next_set
);
433 static void blk_mq_hw_ctx_check_timeout(struct blk_mq_hw_ctx
*hctx
,
435 unsigned int *next_set
)
437 struct blk_mq_timeout_data data
= {
440 .next_set
= next_set
,
444 * Ask the tagging code to iterate busy requests, so we can
445 * check them for timeout.
447 blk_mq_tag_busy_iter(hctx
->tags
, blk_mq_timeout_check
, &data
);
450 static void blk_mq_rq_timer(unsigned long data
)
452 struct request_queue
*q
= (struct request_queue
*) data
;
453 struct blk_mq_hw_ctx
*hctx
;
454 unsigned long next
= 0;
457 queue_for_each_hw_ctx(q
, hctx
, i
)
458 blk_mq_hw_ctx_check_timeout(hctx
, &next
, &next_set
);
461 mod_timer(&q
->timeout
, round_jiffies_up(next
));
465 * Reverse check our software queue for entries that we could potentially
466 * merge with. Currently includes a hand-wavy stop count of 8, to not spend
467 * too much time checking for merges.
469 static bool blk_mq_attempt_merge(struct request_queue
*q
,
470 struct blk_mq_ctx
*ctx
, struct bio
*bio
)
475 list_for_each_entry_reverse(rq
, &ctx
->rq_list
, queuelist
) {
481 if (!blk_rq_merge_ok(rq
, bio
))
484 el_ret
= blk_try_merge(rq
, bio
);
485 if (el_ret
== ELEVATOR_BACK_MERGE
) {
486 if (bio_attempt_back_merge(q
, rq
, bio
)) {
491 } else if (el_ret
== ELEVATOR_FRONT_MERGE
) {
492 if (bio_attempt_front_merge(q
, rq
, bio
)) {
503 void blk_mq_add_timer(struct request
*rq
)
505 __blk_add_timer(rq
, NULL
);
509 * Run this hardware queue, pulling any software queues mapped to it in.
510 * Note that this function currently has various problems around ordering
511 * of IO. In particular, we'd like FIFO behaviour on handling existing
512 * items on the hctx->dispatch list. Ignore that for now.
514 static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx
*hctx
)
516 struct request_queue
*q
= hctx
->queue
;
517 struct blk_mq_ctx
*ctx
;
522 WARN_ON(!preempt_count());
524 if (unlikely(test_bit(BLK_MQ_S_STOPPED
, &hctx
->state
)))
530 * Touch any software queue that has pending entries.
532 for_each_set_bit(bit
, hctx
->ctx_map
, hctx
->nr_ctx
) {
533 clear_bit(bit
, hctx
->ctx_map
);
534 ctx
= hctx
->ctxs
[bit
];
535 BUG_ON(bit
!= ctx
->index_hw
);
537 spin_lock(&ctx
->lock
);
538 list_splice_tail_init(&ctx
->rq_list
, &rq_list
);
539 spin_unlock(&ctx
->lock
);
543 * If we have previous entries on our dispatch list, grab them
544 * and stuff them at the front for more fair dispatch.
546 if (!list_empty_careful(&hctx
->dispatch
)) {
547 spin_lock(&hctx
->lock
);
548 if (!list_empty(&hctx
->dispatch
))
549 list_splice_init(&hctx
->dispatch
, &rq_list
);
550 spin_unlock(&hctx
->lock
);
554 * Delete and return all entries from our dispatch list
559 * Now process all the entries, sending them to the driver.
561 while (!list_empty(&rq_list
)) {
564 rq
= list_first_entry(&rq_list
, struct request
, queuelist
);
565 list_del_init(&rq
->queuelist
);
567 blk_mq_start_request(rq
, list_empty(&rq_list
));
569 ret
= q
->mq_ops
->queue_rq(hctx
, rq
);
571 case BLK_MQ_RQ_QUEUE_OK
:
574 case BLK_MQ_RQ_QUEUE_BUSY
:
576 * FIXME: we should have a mechanism to stop the queue
577 * like blk_stop_queue, otherwise we will waste cpu
580 list_add(&rq
->queuelist
, &rq_list
);
581 blk_mq_requeue_request(rq
);
584 pr_err("blk-mq: bad return on queue: %d\n", ret
);
585 case BLK_MQ_RQ_QUEUE_ERROR
:
587 blk_mq_end_io(rq
, rq
->errors
);
591 if (ret
== BLK_MQ_RQ_QUEUE_BUSY
)
596 hctx
->dispatched
[0]++;
597 else if (queued
< (1 << (BLK_MQ_MAX_DISPATCH_ORDER
- 1)))
598 hctx
->dispatched
[ilog2(queued
) + 1]++;
601 * Any items that need requeuing? Stuff them into hctx->dispatch,
602 * that is where we will continue on next queue run.
604 if (!list_empty(&rq_list
)) {
605 spin_lock(&hctx
->lock
);
606 list_splice(&rq_list
, &hctx
->dispatch
);
607 spin_unlock(&hctx
->lock
);
611 void blk_mq_run_hw_queue(struct blk_mq_hw_ctx
*hctx
, bool async
)
613 if (unlikely(test_bit(BLK_MQ_S_STOPPED
, &hctx
->state
)))
616 if (!async
&& cpumask_test_cpu(smp_processor_id(), hctx
->cpumask
))
617 __blk_mq_run_hw_queue(hctx
);
618 else if (hctx
->queue
->nr_hw_queues
== 1)
619 kblockd_schedule_delayed_work(&hctx
->delayed_work
, 0);
624 * It'd be great if the workqueue API had a way to pass
625 * in a mask and had some smarts for more clever placement
626 * than the first CPU. Or we could round-robin here. For now,
627 * just queue on the first CPU.
629 cpu
= cpumask_first(hctx
->cpumask
);
630 kblockd_schedule_delayed_work_on(cpu
, &hctx
->delayed_work
, 0);
634 void blk_mq_run_queues(struct request_queue
*q
, bool async
)
636 struct blk_mq_hw_ctx
*hctx
;
639 queue_for_each_hw_ctx(q
, hctx
, i
) {
640 if ((!blk_mq_hctx_has_pending(hctx
) &&
641 list_empty_careful(&hctx
->dispatch
)) ||
642 test_bit(BLK_MQ_S_STOPPED
, &hctx
->state
))
646 blk_mq_run_hw_queue(hctx
, async
);
650 EXPORT_SYMBOL(blk_mq_run_queues
);
652 void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx
*hctx
)
654 cancel_delayed_work(&hctx
->delayed_work
);
655 set_bit(BLK_MQ_S_STOPPED
, &hctx
->state
);
657 EXPORT_SYMBOL(blk_mq_stop_hw_queue
);
659 void blk_mq_stop_hw_queues(struct request_queue
*q
)
661 struct blk_mq_hw_ctx
*hctx
;
664 queue_for_each_hw_ctx(q
, hctx
, i
)
665 blk_mq_stop_hw_queue(hctx
);
667 EXPORT_SYMBOL(blk_mq_stop_hw_queues
);
669 void blk_mq_start_hw_queue(struct blk_mq_hw_ctx
*hctx
)
671 clear_bit(BLK_MQ_S_STOPPED
, &hctx
->state
);
674 __blk_mq_run_hw_queue(hctx
);
677 EXPORT_SYMBOL(blk_mq_start_hw_queue
);
679 void blk_mq_start_stopped_hw_queues(struct request_queue
*q
)
681 struct blk_mq_hw_ctx
*hctx
;
684 queue_for_each_hw_ctx(q
, hctx
, i
) {
685 if (!test_bit(BLK_MQ_S_STOPPED
, &hctx
->state
))
688 clear_bit(BLK_MQ_S_STOPPED
, &hctx
->state
);
690 blk_mq_run_hw_queue(hctx
, true);
694 EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues
);
696 static void blk_mq_work_fn(struct work_struct
*work
)
698 struct blk_mq_hw_ctx
*hctx
;
700 hctx
= container_of(work
, struct blk_mq_hw_ctx
, delayed_work
.work
);
703 __blk_mq_run_hw_queue(hctx
);
707 static void __blk_mq_insert_request(struct blk_mq_hw_ctx
*hctx
,
708 struct request
*rq
, bool at_head
)
710 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
;
712 trace_block_rq_insert(hctx
->queue
, rq
);
715 list_add(&rq
->queuelist
, &ctx
->rq_list
);
717 list_add_tail(&rq
->queuelist
, &ctx
->rq_list
);
718 blk_mq_hctx_mark_pending(hctx
, ctx
);
721 * We do this early, to ensure we are on the right CPU.
723 blk_mq_add_timer(rq
);
726 void blk_mq_insert_request(struct request
*rq
, bool at_head
, bool run_queue
,
729 struct request_queue
*q
= rq
->q
;
730 struct blk_mq_hw_ctx
*hctx
;
731 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
, *current_ctx
;
733 current_ctx
= blk_mq_get_ctx(q
);
734 if (!cpu_online(ctx
->cpu
))
735 rq
->mq_ctx
= ctx
= current_ctx
;
737 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
739 if (rq
->cmd_flags
& (REQ_FLUSH
| REQ_FUA
) &&
740 !(rq
->cmd_flags
& (REQ_FLUSH_SEQ
))) {
741 blk_insert_flush(rq
);
743 spin_lock(&ctx
->lock
);
744 __blk_mq_insert_request(hctx
, rq
, at_head
);
745 spin_unlock(&ctx
->lock
);
749 blk_mq_run_hw_queue(hctx
, async
);
751 blk_mq_put_ctx(current_ctx
);
754 static void blk_mq_insert_requests(struct request_queue
*q
,
755 struct blk_mq_ctx
*ctx
,
756 struct list_head
*list
,
761 struct blk_mq_hw_ctx
*hctx
;
762 struct blk_mq_ctx
*current_ctx
;
764 trace_block_unplug(q
, depth
, !from_schedule
);
766 current_ctx
= blk_mq_get_ctx(q
);
768 if (!cpu_online(ctx
->cpu
))
770 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
773 * preemption doesn't flush plug list, so it's possible ctx->cpu is
776 spin_lock(&ctx
->lock
);
777 while (!list_empty(list
)) {
780 rq
= list_first_entry(list
, struct request
, queuelist
);
781 list_del_init(&rq
->queuelist
);
783 __blk_mq_insert_request(hctx
, rq
, false);
785 spin_unlock(&ctx
->lock
);
787 blk_mq_run_hw_queue(hctx
, from_schedule
);
788 blk_mq_put_ctx(current_ctx
);
791 static int plug_ctx_cmp(void *priv
, struct list_head
*a
, struct list_head
*b
)
793 struct request
*rqa
= container_of(a
, struct request
, queuelist
);
794 struct request
*rqb
= container_of(b
, struct request
, queuelist
);
796 return !(rqa
->mq_ctx
< rqb
->mq_ctx
||
797 (rqa
->mq_ctx
== rqb
->mq_ctx
&&
798 blk_rq_pos(rqa
) < blk_rq_pos(rqb
)));
801 void blk_mq_flush_plug_list(struct blk_plug
*plug
, bool from_schedule
)
803 struct blk_mq_ctx
*this_ctx
;
804 struct request_queue
*this_q
;
810 list_splice_init(&plug
->mq_list
, &list
);
812 list_sort(NULL
, &list
, plug_ctx_cmp
);
818 while (!list_empty(&list
)) {
819 rq
= list_entry_rq(list
.next
);
820 list_del_init(&rq
->queuelist
);
822 if (rq
->mq_ctx
!= this_ctx
) {
824 blk_mq_insert_requests(this_q
, this_ctx
,
829 this_ctx
= rq
->mq_ctx
;
835 list_add_tail(&rq
->queuelist
, &ctx_list
);
839 * If 'this_ctx' is set, we know we have entries to complete
840 * on 'ctx_list'. Do those.
843 blk_mq_insert_requests(this_q
, this_ctx
, &ctx_list
, depth
,
848 static void blk_mq_bio_to_request(struct request
*rq
, struct bio
*bio
)
850 init_request_from_bio(rq
, bio
);
851 blk_account_io_start(rq
, 1);
854 static void blk_mq_make_request(struct request_queue
*q
, struct bio
*bio
)
856 struct blk_mq_hw_ctx
*hctx
;
857 struct blk_mq_ctx
*ctx
;
858 const int is_sync
= rw_is_sync(bio
->bi_rw
);
859 const int is_flush_fua
= bio
->bi_rw
& (REQ_FLUSH
| REQ_FUA
);
860 int rw
= bio_data_dir(bio
);
862 unsigned int use_plug
, request_count
= 0;
865 * If we have multiple hardware queues, just go directly to
866 * one of those for sync IO.
868 use_plug
= !is_flush_fua
&& ((q
->nr_hw_queues
== 1) || !is_sync
);
870 blk_queue_bounce(q
, &bio
);
872 if (bio_integrity_enabled(bio
) && bio_integrity_prep(bio
)) {
873 bio_endio(bio
, -EIO
);
877 if (use_plug
&& blk_attempt_plug_merge(q
, bio
, &request_count
))
880 if (blk_mq_queue_enter(q
)) {
881 bio_endio(bio
, -EIO
);
885 ctx
= blk_mq_get_ctx(q
);
886 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
890 trace_block_getrq(q
, bio
, rw
);
891 rq
= __blk_mq_alloc_request(hctx
, GFP_ATOMIC
, false);
893 blk_mq_rq_ctx_init(q
, ctx
, rq
, rw
);
896 trace_block_sleeprq(q
, bio
, rw
);
897 rq
= blk_mq_alloc_request_pinned(q
, rw
, __GFP_WAIT
|GFP_ATOMIC
,
900 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
905 if (unlikely(is_flush_fua
)) {
906 blk_mq_bio_to_request(rq
, bio
);
907 blk_insert_flush(rq
);
912 * A task plug currently exists. Since this is completely lockless,
913 * utilize that to temporarily store requests until the task is
914 * either done or scheduled away.
917 struct blk_plug
*plug
= current
->plug
;
920 blk_mq_bio_to_request(rq
, bio
);
921 if (list_empty(&plug
->mq_list
))
923 else if (request_count
>= BLK_MAX_REQUEST_COUNT
) {
924 blk_flush_plug_list(plug
, false);
927 list_add_tail(&rq
->queuelist
, &plug
->mq_list
);
933 spin_lock(&ctx
->lock
);
935 if ((hctx
->flags
& BLK_MQ_F_SHOULD_MERGE
) &&
936 blk_mq_attempt_merge(q
, ctx
, bio
))
937 __blk_mq_free_request(hctx
, ctx
, rq
);
939 blk_mq_bio_to_request(rq
, bio
);
940 __blk_mq_insert_request(hctx
, rq
, false);
943 spin_unlock(&ctx
->lock
);
946 * For a SYNC request, send it to the hardware immediately. For an
947 * ASYNC request, just ensure that we run it later on. The latter
948 * allows for merging opportunities and more efficient dispatching.
951 blk_mq_run_hw_queue(hctx
, !is_sync
|| is_flush_fua
);
956 * Default mapping to a software queue, since we use one per CPU.
958 struct blk_mq_hw_ctx
*blk_mq_map_queue(struct request_queue
*q
, const int cpu
)
960 return q
->queue_hw_ctx
[q
->mq_map
[cpu
]];
962 EXPORT_SYMBOL(blk_mq_map_queue
);
964 struct blk_mq_hw_ctx
*blk_mq_alloc_single_hw_queue(struct blk_mq_reg
*reg
,
965 unsigned int hctx_index
)
967 return kmalloc_node(sizeof(struct blk_mq_hw_ctx
),
968 GFP_KERNEL
| __GFP_ZERO
, reg
->numa_node
);
970 EXPORT_SYMBOL(blk_mq_alloc_single_hw_queue
);
972 void blk_mq_free_single_hw_queue(struct blk_mq_hw_ctx
*hctx
,
973 unsigned int hctx_index
)
977 EXPORT_SYMBOL(blk_mq_free_single_hw_queue
);
979 static void blk_mq_hctx_notify(void *data
, unsigned long action
,
982 struct blk_mq_hw_ctx
*hctx
= data
;
983 struct request_queue
*q
= hctx
->queue
;
984 struct blk_mq_ctx
*ctx
;
987 if (action
!= CPU_DEAD
&& action
!= CPU_DEAD_FROZEN
)
991 * Move ctx entries to new CPU, if this one is going away.
993 ctx
= __blk_mq_get_ctx(q
, cpu
);
995 spin_lock(&ctx
->lock
);
996 if (!list_empty(&ctx
->rq_list
)) {
997 list_splice_init(&ctx
->rq_list
, &tmp
);
998 clear_bit(ctx
->index_hw
, hctx
->ctx_map
);
1000 spin_unlock(&ctx
->lock
);
1002 if (list_empty(&tmp
))
1005 ctx
= blk_mq_get_ctx(q
);
1006 spin_lock(&ctx
->lock
);
1008 while (!list_empty(&tmp
)) {
1011 rq
= list_first_entry(&tmp
, struct request
, queuelist
);
1013 list_move_tail(&rq
->queuelist
, &ctx
->rq_list
);
1016 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
1017 blk_mq_hctx_mark_pending(hctx
, ctx
);
1019 spin_unlock(&ctx
->lock
);
1021 blk_mq_run_hw_queue(hctx
, true);
1022 blk_mq_put_ctx(ctx
);
1025 static int blk_mq_init_hw_commands(struct blk_mq_hw_ctx
*hctx
,
1026 int (*init
)(void *, struct blk_mq_hw_ctx
*,
1027 struct request
*, unsigned int),
1033 for (i
= 0; i
< hctx
->queue_depth
; i
++) {
1034 struct request
*rq
= hctx
->rqs
[i
];
1036 ret
= init(data
, hctx
, rq
, i
);
1044 int blk_mq_init_commands(struct request_queue
*q
,
1045 int (*init
)(void *, struct blk_mq_hw_ctx
*,
1046 struct request
*, unsigned int),
1049 struct blk_mq_hw_ctx
*hctx
;
1053 queue_for_each_hw_ctx(q
, hctx
, i
) {
1054 ret
= blk_mq_init_hw_commands(hctx
, init
, data
);
1061 EXPORT_SYMBOL(blk_mq_init_commands
);
1063 static void blk_mq_free_hw_commands(struct blk_mq_hw_ctx
*hctx
,
1064 void (*free
)(void *, struct blk_mq_hw_ctx
*,
1065 struct request
*, unsigned int),
1070 for (i
= 0; i
< hctx
->queue_depth
; i
++) {
1071 struct request
*rq
= hctx
->rqs
[i
];
1073 free(data
, hctx
, rq
, i
);
1077 void blk_mq_free_commands(struct request_queue
*q
,
1078 void (*free
)(void *, struct blk_mq_hw_ctx
*,
1079 struct request
*, unsigned int),
1082 struct blk_mq_hw_ctx
*hctx
;
1085 queue_for_each_hw_ctx(q
, hctx
, i
)
1086 blk_mq_free_hw_commands(hctx
, free
, data
);
1088 EXPORT_SYMBOL(blk_mq_free_commands
);
1090 static void blk_mq_free_rq_map(struct blk_mq_hw_ctx
*hctx
)
1094 while (!list_empty(&hctx
->page_list
)) {
1095 page
= list_first_entry(&hctx
->page_list
, struct page
, lru
);
1096 list_del_init(&page
->lru
);
1097 __free_pages(page
, page
->private);
1103 blk_mq_free_tags(hctx
->tags
);
1106 static size_t order_to_size(unsigned int order
)
1108 size_t ret
= PAGE_SIZE
;
1116 static int blk_mq_init_rq_map(struct blk_mq_hw_ctx
*hctx
,
1117 unsigned int reserved_tags
, int node
)
1119 unsigned int i
, j
, entries_per_page
, max_order
= 4;
1120 size_t rq_size
, left
;
1122 INIT_LIST_HEAD(&hctx
->page_list
);
1124 hctx
->rqs
= kmalloc_node(hctx
->queue_depth
* sizeof(struct request
*),
1130 * rq_size is the size of the request plus driver payload, rounded
1131 * to the cacheline size
1133 rq_size
= round_up(sizeof(struct request
) + hctx
->cmd_size
,
1135 left
= rq_size
* hctx
->queue_depth
;
1137 for (i
= 0; i
< hctx
->queue_depth
;) {
1138 int this_order
= max_order
;
1143 while (left
< order_to_size(this_order
- 1) && this_order
)
1147 page
= alloc_pages_node(node
, GFP_KERNEL
, this_order
);
1152 if (order_to_size(this_order
) < rq_size
)
1159 page
->private = this_order
;
1160 list_add_tail(&page
->lru
, &hctx
->page_list
);
1162 p
= page_address(page
);
1163 entries_per_page
= order_to_size(this_order
) / rq_size
;
1164 to_do
= min(entries_per_page
, hctx
->queue_depth
- i
);
1165 left
-= to_do
* rq_size
;
1166 for (j
= 0; j
< to_do
; j
++) {
1168 blk_mq_rq_init(hctx
, hctx
->rqs
[i
]);
1174 if (i
< (reserved_tags
+ BLK_MQ_TAG_MIN
))
1176 else if (i
!= hctx
->queue_depth
) {
1177 hctx
->queue_depth
= i
;
1178 pr_warn("%s: queue depth set to %u because of low memory\n",
1182 hctx
->tags
= blk_mq_init_tags(hctx
->queue_depth
, reserved_tags
, node
);
1185 blk_mq_free_rq_map(hctx
);
1192 static int blk_mq_init_hw_queues(struct request_queue
*q
,
1193 struct blk_mq_reg
*reg
, void *driver_data
)
1195 struct blk_mq_hw_ctx
*hctx
;
1199 * Initialize hardware queues
1201 queue_for_each_hw_ctx(q
, hctx
, i
) {
1202 unsigned int num_maps
;
1205 node
= hctx
->numa_node
;
1206 if (node
== NUMA_NO_NODE
)
1207 node
= hctx
->numa_node
= reg
->numa_node
;
1209 INIT_DELAYED_WORK(&hctx
->delayed_work
, blk_mq_work_fn
);
1210 spin_lock_init(&hctx
->lock
);
1211 INIT_LIST_HEAD(&hctx
->dispatch
);
1213 hctx
->queue_num
= i
;
1214 hctx
->flags
= reg
->flags
;
1215 hctx
->queue_depth
= reg
->queue_depth
;
1216 hctx
->cmd_size
= reg
->cmd_size
;
1218 blk_mq_init_cpu_notifier(&hctx
->cpu_notifier
,
1219 blk_mq_hctx_notify
, hctx
);
1220 blk_mq_register_cpu_notifier(&hctx
->cpu_notifier
);
1222 if (blk_mq_init_rq_map(hctx
, reg
->reserved_tags
, node
))
1226 * Allocate space for all possible cpus to avoid allocation in
1229 hctx
->ctxs
= kmalloc_node(nr_cpu_ids
* sizeof(void *),
1234 num_maps
= ALIGN(nr_cpu_ids
, BITS_PER_LONG
) / BITS_PER_LONG
;
1235 hctx
->ctx_map
= kzalloc_node(num_maps
* sizeof(unsigned long),
1240 hctx
->nr_ctx_map
= num_maps
;
1243 if (reg
->ops
->init_hctx
&&
1244 reg
->ops
->init_hctx(hctx
, driver_data
, i
))
1248 if (i
== q
->nr_hw_queues
)
1254 queue_for_each_hw_ctx(q
, hctx
, j
) {
1258 if (reg
->ops
->exit_hctx
)
1259 reg
->ops
->exit_hctx(hctx
, j
);
1261 blk_mq_unregister_cpu_notifier(&hctx
->cpu_notifier
);
1262 blk_mq_free_rq_map(hctx
);
1269 static void blk_mq_init_cpu_queues(struct request_queue
*q
,
1270 unsigned int nr_hw_queues
)
1274 for_each_possible_cpu(i
) {
1275 struct blk_mq_ctx
*__ctx
= per_cpu_ptr(q
->queue_ctx
, i
);
1276 struct blk_mq_hw_ctx
*hctx
;
1278 memset(__ctx
, 0, sizeof(*__ctx
));
1280 spin_lock_init(&__ctx
->lock
);
1281 INIT_LIST_HEAD(&__ctx
->rq_list
);
1284 /* If the cpu isn't online, the cpu is mapped to first hctx */
1288 hctx
= q
->mq_ops
->map_queue(q
, i
);
1289 cpumask_set_cpu(i
, hctx
->cpumask
);
1293 * Set local node, IFF we have more than one hw queue. If
1294 * not, we remain on the home node of the device
1296 if (nr_hw_queues
> 1 && hctx
->numa_node
== NUMA_NO_NODE
)
1297 hctx
->numa_node
= cpu_to_node(i
);
1301 static void blk_mq_map_swqueue(struct request_queue
*q
)
1304 struct blk_mq_hw_ctx
*hctx
;
1305 struct blk_mq_ctx
*ctx
;
1307 queue_for_each_hw_ctx(q
, hctx
, i
) {
1308 cpumask_clear(hctx
->cpumask
);
1313 * Map software to hardware queues
1315 queue_for_each_ctx(q
, ctx
, i
) {
1316 /* If the cpu isn't online, the cpu is mapped to first hctx */
1320 hctx
= q
->mq_ops
->map_queue(q
, i
);
1321 cpumask_set_cpu(i
, hctx
->cpumask
);
1322 ctx
->index_hw
= hctx
->nr_ctx
;
1323 hctx
->ctxs
[hctx
->nr_ctx
++] = ctx
;
1327 struct request_queue
*blk_mq_init_queue(struct blk_mq_reg
*reg
,
1330 struct blk_mq_hw_ctx
**hctxs
;
1331 struct blk_mq_ctx
*ctx
;
1332 struct request_queue
*q
;
1335 if (!reg
->nr_hw_queues
||
1336 !reg
->ops
->queue_rq
|| !reg
->ops
->map_queue
||
1337 !reg
->ops
->alloc_hctx
|| !reg
->ops
->free_hctx
)
1338 return ERR_PTR(-EINVAL
);
1340 if (!reg
->queue_depth
)
1341 reg
->queue_depth
= BLK_MQ_MAX_DEPTH
;
1342 else if (reg
->queue_depth
> BLK_MQ_MAX_DEPTH
) {
1343 pr_err("blk-mq: queuedepth too large (%u)\n", reg
->queue_depth
);
1344 reg
->queue_depth
= BLK_MQ_MAX_DEPTH
;
1347 if (reg
->queue_depth
< (reg
->reserved_tags
+ BLK_MQ_TAG_MIN
))
1348 return ERR_PTR(-EINVAL
);
1350 ctx
= alloc_percpu(struct blk_mq_ctx
);
1352 return ERR_PTR(-ENOMEM
);
1354 hctxs
= kmalloc_node(reg
->nr_hw_queues
* sizeof(*hctxs
), GFP_KERNEL
,
1360 for (i
= 0; i
< reg
->nr_hw_queues
; i
++) {
1361 hctxs
[i
] = reg
->ops
->alloc_hctx(reg
, i
);
1365 if (!zalloc_cpumask_var(&hctxs
[i
]->cpumask
, GFP_KERNEL
))
1368 hctxs
[i
]->numa_node
= NUMA_NO_NODE
;
1369 hctxs
[i
]->queue_num
= i
;
1372 q
= blk_alloc_queue_node(GFP_KERNEL
, reg
->numa_node
);
1376 q
->mq_map
= blk_mq_make_queue_map(reg
);
1380 setup_timer(&q
->timeout
, blk_mq_rq_timer
, (unsigned long) q
);
1381 blk_queue_rq_timeout(q
, 30000);
1383 q
->nr_queues
= nr_cpu_ids
;
1384 q
->nr_hw_queues
= reg
->nr_hw_queues
;
1387 q
->queue_hw_ctx
= hctxs
;
1389 q
->mq_ops
= reg
->ops
;
1390 q
->queue_flags
|= QUEUE_FLAG_MQ_DEFAULT
;
1392 q
->sg_reserved_size
= INT_MAX
;
1394 blk_queue_make_request(q
, blk_mq_make_request
);
1395 blk_queue_rq_timed_out(q
, reg
->ops
->timeout
);
1397 blk_queue_rq_timeout(q
, reg
->timeout
);
1399 if (reg
->ops
->complete
)
1400 blk_queue_softirq_done(q
, reg
->ops
->complete
);
1402 blk_mq_init_flush(q
);
1403 blk_mq_init_cpu_queues(q
, reg
->nr_hw_queues
);
1405 q
->flush_rq
= kzalloc(round_up(sizeof(struct request
) + reg
->cmd_size
,
1406 cache_line_size()), GFP_KERNEL
);
1410 if (blk_mq_init_hw_queues(q
, reg
, driver_data
))
1413 blk_mq_map_swqueue(q
);
1415 mutex_lock(&all_q_mutex
);
1416 list_add_tail(&q
->all_q_node
, &all_q_list
);
1417 mutex_unlock(&all_q_mutex
);
1426 blk_cleanup_queue(q
);
1428 for (i
= 0; i
< reg
->nr_hw_queues
; i
++) {
1431 free_cpumask_var(hctxs
[i
]->cpumask
);
1432 reg
->ops
->free_hctx(hctxs
[i
], i
);
1437 return ERR_PTR(-ENOMEM
);
1439 EXPORT_SYMBOL(blk_mq_init_queue
);
1441 void blk_mq_free_queue(struct request_queue
*q
)
1443 struct blk_mq_hw_ctx
*hctx
;
1446 queue_for_each_hw_ctx(q
, hctx
, i
) {
1447 kfree(hctx
->ctx_map
);
1449 blk_mq_free_rq_map(hctx
);
1450 blk_mq_unregister_cpu_notifier(&hctx
->cpu_notifier
);
1451 if (q
->mq_ops
->exit_hctx
)
1452 q
->mq_ops
->exit_hctx(hctx
, i
);
1453 free_cpumask_var(hctx
->cpumask
);
1454 q
->mq_ops
->free_hctx(hctx
, i
);
1457 free_percpu(q
->queue_ctx
);
1458 kfree(q
->queue_hw_ctx
);
1461 q
->queue_ctx
= NULL
;
1462 q
->queue_hw_ctx
= NULL
;
1465 mutex_lock(&all_q_mutex
);
1466 list_del_init(&q
->all_q_node
);
1467 mutex_unlock(&all_q_mutex
);
1470 /* Basically redo blk_mq_init_queue with queue frozen */
1471 static void blk_mq_queue_reinit(struct request_queue
*q
)
1473 blk_mq_freeze_queue(q
);
1475 blk_mq_update_queue_map(q
->mq_map
, q
->nr_hw_queues
);
1478 * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
1479 * we should change hctx numa_node according to new topology (this
1480 * involves free and re-allocate memory, worthy doing?)
1483 blk_mq_map_swqueue(q
);
1485 blk_mq_unfreeze_queue(q
);
1488 static int blk_mq_queue_reinit_notify(struct notifier_block
*nb
,
1489 unsigned long action
, void *hcpu
)
1491 struct request_queue
*q
;
1494 * Before new mapping is established, hotadded cpu might already start
1495 * handling requests. This doesn't break anything as we map offline
1496 * CPUs to first hardware queue. We will re-init queue below to get
1499 if (action
!= CPU_DEAD
&& action
!= CPU_DEAD_FROZEN
&&
1500 action
!= CPU_ONLINE
&& action
!= CPU_ONLINE_FROZEN
)
1503 mutex_lock(&all_q_mutex
);
1504 list_for_each_entry(q
, &all_q_list
, all_q_node
)
1505 blk_mq_queue_reinit(q
);
1506 mutex_unlock(&all_q_mutex
);
1510 void blk_mq_disable_hotplug(void)
1512 mutex_lock(&all_q_mutex
);
1515 void blk_mq_enable_hotplug(void)
1517 mutex_unlock(&all_q_mutex
);
1520 static int __init
blk_mq_init(void)
1524 /* Must be called after percpu_counter_hotcpu_callback() */
1525 hotcpu_notifier(blk_mq_queue_reinit_notify
, -10);
1529 subsys_initcall(blk_mq_init
);