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 struct blk_mq_ctx
*ctx
;
35 ctx
= per_cpu_ptr(q
->queue_ctx
, cpu
);
36 spin_lock(&ctx
->cpu_lock
);
41 * This assumes per-cpu software queueing queues. They could be per-node
42 * as well, for instance. For now this is hardcoded as-is. Note that we don't
43 * care about preemption, since we know the ctx's are persistent. This does
44 * mean that we can't rely on ctx always matching the currently running CPU.
46 static struct blk_mq_ctx
*blk_mq_get_ctx(struct request_queue
*q
)
48 return __blk_mq_get_ctx(q
, get_cpu_light());
51 static void __blk_mq_put_ctx(struct blk_mq_ctx
*ctx
)
53 spin_unlock(&ctx
->cpu_lock
);
56 static void blk_mq_put_ctx(struct blk_mq_ctx
*ctx
)
58 __blk_mq_put_ctx(ctx
);
63 * Check if any of the ctx's have pending work in this hardware queue
65 static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx
*hctx
)
69 for (i
= 0; i
< hctx
->nr_ctx_map
; i
++)
77 * Mark this ctx as having pending work in this hardware queue
79 static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx
*hctx
,
80 struct blk_mq_ctx
*ctx
)
82 if (!test_bit(ctx
->index_hw
, hctx
->ctx_map
))
83 set_bit(ctx
->index_hw
, hctx
->ctx_map
);
86 static struct request
*__blk_mq_alloc_request(struct blk_mq_hw_ctx
*hctx
,
87 gfp_t gfp
, bool reserved
)
92 tag
= blk_mq_get_tag(hctx
->tags
, gfp
, reserved
);
93 if (tag
!= BLK_MQ_TAG_FAIL
) {
103 static int blk_mq_queue_enter(struct request_queue
*q
)
107 __percpu_counter_add(&q
->mq_usage_counter
, 1, 1000000);
109 /* we have problems to freeze the queue if it's initializing */
110 if (!blk_queue_bypass(q
) || !blk_queue_init_done(q
))
113 __percpu_counter_add(&q
->mq_usage_counter
, -1, 1000000);
115 spin_lock_irq(q
->queue_lock
);
116 ret
= wait_event_interruptible_lock_irq(q
->mq_freeze_wq
,
117 !blk_queue_bypass(q
) || blk_queue_dying(q
),
119 /* inc usage with lock hold to avoid freeze_queue runs here */
120 if (!ret
&& !blk_queue_dying(q
))
121 __percpu_counter_add(&q
->mq_usage_counter
, 1, 1000000);
122 else if (blk_queue_dying(q
))
124 spin_unlock_irq(q
->queue_lock
);
129 static void blk_mq_queue_exit(struct request_queue
*q
)
131 __percpu_counter_add(&q
->mq_usage_counter
, -1, 1000000);
134 static void __blk_mq_drain_queue(struct request_queue
*q
)
139 spin_lock_irq(q
->queue_lock
);
140 count
= percpu_counter_sum(&q
->mq_usage_counter
);
141 spin_unlock_irq(q
->queue_lock
);
145 blk_mq_run_queues(q
, false);
151 * Guarantee no request is in use, so we can change any data structure of
152 * the queue afterward.
154 static void blk_mq_freeze_queue(struct request_queue
*q
)
158 spin_lock_irq(q
->queue_lock
);
159 drain
= !q
->bypass_depth
++;
160 queue_flag_set(QUEUE_FLAG_BYPASS
, q
);
161 spin_unlock_irq(q
->queue_lock
);
164 __blk_mq_drain_queue(q
);
167 void blk_mq_drain_queue(struct request_queue
*q
)
169 __blk_mq_drain_queue(q
);
172 static void blk_mq_unfreeze_queue(struct request_queue
*q
)
176 spin_lock_irq(q
->queue_lock
);
177 if (!--q
->bypass_depth
) {
178 queue_flag_clear(QUEUE_FLAG_BYPASS
, q
);
181 WARN_ON_ONCE(q
->bypass_depth
< 0);
182 spin_unlock_irq(q
->queue_lock
);
184 wake_up_all(&q
->mq_freeze_wq
);
187 bool blk_mq_can_queue(struct blk_mq_hw_ctx
*hctx
)
189 return blk_mq_has_free_tags(hctx
->tags
);
191 EXPORT_SYMBOL(blk_mq_can_queue
);
193 static void blk_mq_rq_ctx_init(struct request_queue
*q
, struct blk_mq_ctx
*ctx
,
194 struct request
*rq
, unsigned int rw_flags
)
196 if (blk_queue_io_stat(q
))
197 rw_flags
|= REQ_IO_STAT
;
200 rq
->cmd_flags
= rw_flags
;
201 rq
->start_time
= jiffies
;
202 set_start_time_ns(rq
);
203 ctx
->rq_dispatched
[rw_is_sync(rw_flags
)]++;
206 static struct request
*blk_mq_alloc_request_pinned(struct request_queue
*q
,
213 struct blk_mq_ctx
*ctx
= blk_mq_get_ctx(q
);
214 struct blk_mq_hw_ctx
*hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
216 rq
= __blk_mq_alloc_request(hctx
, gfp
& ~__GFP_WAIT
, reserved
);
218 blk_mq_rq_ctx_init(q
, ctx
, rq
, rw
);
223 if (!(gfp
& __GFP_WAIT
))
226 __blk_mq_run_hw_queue(hctx
);
227 blk_mq_wait_for_tags(hctx
->tags
);
233 struct request
*blk_mq_alloc_request(struct request_queue
*q
, int rw
, gfp_t gfp
)
237 if (blk_mq_queue_enter(q
))
240 rq
= blk_mq_alloc_request_pinned(q
, rw
, gfp
, false);
242 blk_mq_put_ctx(rq
->mq_ctx
);
246 struct request
*blk_mq_alloc_reserved_request(struct request_queue
*q
, int rw
,
251 if (blk_mq_queue_enter(q
))
254 rq
= blk_mq_alloc_request_pinned(q
, rw
, gfp
, true);
256 blk_mq_put_ctx(rq
->mq_ctx
);
259 EXPORT_SYMBOL(blk_mq_alloc_reserved_request
);
262 * Re-init and set pdu, if we have it
264 void blk_mq_rq_init(struct blk_mq_hw_ctx
*hctx
, struct request
*rq
)
266 blk_rq_init(hctx
->queue
, rq
);
269 rq
->special
= blk_mq_rq_to_pdu(rq
);
272 static void __blk_mq_free_request(struct blk_mq_hw_ctx
*hctx
,
273 struct blk_mq_ctx
*ctx
, struct request
*rq
)
275 const int tag
= rq
->tag
;
276 struct request_queue
*q
= rq
->q
;
278 blk_mq_rq_init(hctx
, rq
);
279 blk_mq_put_tag(hctx
->tags
, tag
);
281 blk_mq_queue_exit(q
);
284 void blk_mq_free_request(struct request
*rq
)
286 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
;
287 struct blk_mq_hw_ctx
*hctx
;
288 struct request_queue
*q
= rq
->q
;
290 ctx
->rq_completed
[rq_is_sync(rq
)]++;
292 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
293 __blk_mq_free_request(hctx
, ctx
, rq
);
296 bool blk_mq_end_io_partial(struct request
*rq
, int error
, unsigned int nr_bytes
)
298 if (blk_update_request(rq
, error
, blk_rq_bytes(rq
)))
301 blk_account_io_done(rq
);
304 rq
->end_io(rq
, error
);
306 blk_mq_free_request(rq
);
309 EXPORT_SYMBOL(blk_mq_end_io_partial
);
311 static void __blk_mq_complete_request_remote(void *data
)
313 struct request
*rq
= data
;
315 rq
->q
->softirq_done_fn(rq
);
318 void __blk_mq_complete_request(struct request
*rq
)
320 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
;
323 if (!ctx
->ipi_redirect
) {
324 rq
->q
->softirq_done_fn(rq
);
329 if (cpu
!= ctx
->cpu
&& cpu_online(ctx
->cpu
)) {
330 rq
->csd
.func
= __blk_mq_complete_request_remote
;
333 __smp_call_function_single(ctx
->cpu
, &rq
->csd
, 0);
335 rq
->q
->softirq_done_fn(rq
);
341 * blk_mq_complete_request - end I/O on a request
342 * @rq: the request being processed
345 * Ends all I/O on a request. It does not handle partial completions.
346 * The actual completion happens out-of-order, through a IPI handler.
348 void blk_mq_complete_request(struct request
*rq
)
350 if (unlikely(blk_should_fake_timeout(rq
->q
)))
352 if (!blk_mark_rq_complete(rq
))
353 __blk_mq_complete_request(rq
);
355 EXPORT_SYMBOL(blk_mq_complete_request
);
357 static void blk_mq_start_request(struct request
*rq
, bool last
)
359 struct request_queue
*q
= rq
->q
;
361 trace_block_rq_issue(q
, rq
);
364 * Just mark start time and set the started bit. Due to memory
365 * ordering, we know we'll see the correct deadline as long as
366 * REQ_ATOMIC_STARTED is seen.
368 rq
->deadline
= jiffies
+ q
->rq_timeout
;
369 set_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
);
371 if (q
->dma_drain_size
&& blk_rq_bytes(rq
)) {
373 * Make sure space for the drain appears. We know we can do
374 * this because max_hw_segments has been adjusted to be one
375 * fewer than the device can handle.
377 rq
->nr_phys_segments
++;
381 * Flag the last request in the series so that drivers know when IO
382 * should be kicked off, if they don't do it on a per-request basis.
384 * Note: the flag isn't the only condition drivers should do kick off.
385 * If drive is busy, the last request might not have the bit set.
388 rq
->cmd_flags
|= REQ_END
;
391 static void blk_mq_requeue_request(struct request
*rq
)
393 struct request_queue
*q
= rq
->q
;
395 trace_block_rq_requeue(q
, rq
);
396 clear_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
);
398 rq
->cmd_flags
&= ~REQ_END
;
400 if (q
->dma_drain_size
&& blk_rq_bytes(rq
))
401 rq
->nr_phys_segments
--;
404 struct blk_mq_timeout_data
{
405 struct blk_mq_hw_ctx
*hctx
;
407 unsigned int *next_set
;
410 static void blk_mq_timeout_check(void *__data
, unsigned long *free_tags
)
412 struct blk_mq_timeout_data
*data
= __data
;
413 struct blk_mq_hw_ctx
*hctx
= data
->hctx
;
416 /* It may not be in flight yet (this is where
417 * the REQ_ATOMIC_STARTED flag comes in). The requests are
418 * statically allocated, so we know it's always safe to access the
419 * memory associated with a bit offset into ->rqs[].
425 tag
= find_next_zero_bit(free_tags
, hctx
->queue_depth
, tag
);
426 if (tag
>= hctx
->queue_depth
)
429 rq
= hctx
->rqs
[tag
++];
431 if (!test_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
))
434 blk_rq_check_expired(rq
, data
->next
, data
->next_set
);
438 static void blk_mq_hw_ctx_check_timeout(struct blk_mq_hw_ctx
*hctx
,
440 unsigned int *next_set
)
442 struct blk_mq_timeout_data data
= {
445 .next_set
= next_set
,
449 * Ask the tagging code to iterate busy requests, so we can
450 * check them for timeout.
452 blk_mq_tag_busy_iter(hctx
->tags
, blk_mq_timeout_check
, &data
);
455 static void blk_mq_rq_timer(unsigned long data
)
457 struct request_queue
*q
= (struct request_queue
*) data
;
458 struct blk_mq_hw_ctx
*hctx
;
459 unsigned long next
= 0;
462 queue_for_each_hw_ctx(q
, hctx
, i
)
463 blk_mq_hw_ctx_check_timeout(hctx
, &next
, &next_set
);
466 mod_timer(&q
->timeout
, round_jiffies_up(next
));
470 * Reverse check our software queue for entries that we could potentially
471 * merge with. Currently includes a hand-wavy stop count of 8, to not spend
472 * too much time checking for merges.
474 static bool blk_mq_attempt_merge(struct request_queue
*q
,
475 struct blk_mq_ctx
*ctx
, struct bio
*bio
)
480 list_for_each_entry_reverse(rq
, &ctx
->rq_list
, queuelist
) {
486 if (!blk_rq_merge_ok(rq
, bio
))
489 el_ret
= blk_try_merge(rq
, bio
);
490 if (el_ret
== ELEVATOR_BACK_MERGE
) {
491 if (bio_attempt_back_merge(q
, rq
, bio
)) {
496 } else if (el_ret
== ELEVATOR_FRONT_MERGE
) {
497 if (bio_attempt_front_merge(q
, rq
, bio
)) {
508 void blk_mq_add_timer(struct request
*rq
)
510 __blk_add_timer(rq
, NULL
);
514 * Run this hardware queue, pulling any software queues mapped to it in.
515 * Note that this function currently has various problems around ordering
516 * of IO. In particular, we'd like FIFO behaviour on handling existing
517 * items on the hctx->dispatch list. Ignore that for now.
519 static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx
*hctx
)
521 struct request_queue
*q
= hctx
->queue
;
522 struct blk_mq_ctx
*ctx
;
527 if (unlikely(test_bit(BLK_MQ_S_STOPPED
, &hctx
->flags
)))
533 * Touch any software queue that has pending entries.
535 for_each_set_bit(bit
, hctx
->ctx_map
, hctx
->nr_ctx
) {
536 clear_bit(bit
, hctx
->ctx_map
);
537 ctx
= hctx
->ctxs
[bit
];
538 BUG_ON(bit
!= ctx
->index_hw
);
540 spin_lock(&ctx
->lock
);
541 list_splice_tail_init(&ctx
->rq_list
, &rq_list
);
542 spin_unlock(&ctx
->lock
);
546 * If we have previous entries on our dispatch list, grab them
547 * and stuff them at the front for more fair dispatch.
549 if (!list_empty_careful(&hctx
->dispatch
)) {
550 spin_lock(&hctx
->lock
);
551 if (!list_empty(&hctx
->dispatch
))
552 list_splice_init(&hctx
->dispatch
, &rq_list
);
553 spin_unlock(&hctx
->lock
);
557 * Delete and return all entries from our dispatch list
562 * Now process all the entries, sending them to the driver.
564 while (!list_empty(&rq_list
)) {
567 rq
= list_first_entry(&rq_list
, struct request
, queuelist
);
568 list_del_init(&rq
->queuelist
);
570 blk_mq_start_request(rq
, list_empty(&rq_list
));
572 ret
= q
->mq_ops
->queue_rq(hctx
, rq
);
574 case BLK_MQ_RQ_QUEUE_OK
:
577 case BLK_MQ_RQ_QUEUE_BUSY
:
579 * FIXME: we should have a mechanism to stop the queue
580 * like blk_stop_queue, otherwise we will waste cpu
583 list_add(&rq
->queuelist
, &rq_list
);
584 blk_mq_requeue_request(rq
);
587 pr_err("blk-mq: bad return on queue: %d\n", ret
);
588 case BLK_MQ_RQ_QUEUE_ERROR
:
590 blk_mq_end_io(rq
, rq
->errors
);
594 if (ret
== BLK_MQ_RQ_QUEUE_BUSY
)
599 hctx
->dispatched
[0]++;
600 else if (queued
< (1 << (BLK_MQ_MAX_DISPATCH_ORDER
- 1)))
601 hctx
->dispatched
[ilog2(queued
) + 1]++;
604 * Any items that need requeuing? Stuff them into hctx->dispatch,
605 * that is where we will continue on next queue run.
607 if (!list_empty(&rq_list
)) {
608 spin_lock(&hctx
->lock
);
609 list_splice(&rq_list
, &hctx
->dispatch
);
610 spin_unlock(&hctx
->lock
);
614 void blk_mq_run_hw_queue(struct blk_mq_hw_ctx
*hctx
, bool async
)
616 if (unlikely(test_bit(BLK_MQ_S_STOPPED
, &hctx
->flags
)))
620 __blk_mq_run_hw_queue(hctx
);
622 struct request_queue
*q
= hctx
->queue
;
624 kblockd_schedule_delayed_work(q
, &hctx
->delayed_work
, 0);
628 void blk_mq_run_queues(struct request_queue
*q
, bool async
)
630 struct blk_mq_hw_ctx
*hctx
;
633 queue_for_each_hw_ctx(q
, hctx
, i
) {
634 if ((!blk_mq_hctx_has_pending(hctx
) &&
635 list_empty_careful(&hctx
->dispatch
)) ||
636 test_bit(BLK_MQ_S_STOPPED
, &hctx
->flags
))
639 blk_mq_run_hw_queue(hctx
, async
);
642 EXPORT_SYMBOL(blk_mq_run_queues
);
644 void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx
*hctx
)
646 cancel_delayed_work(&hctx
->delayed_work
);
647 set_bit(BLK_MQ_S_STOPPED
, &hctx
->state
);
649 EXPORT_SYMBOL(blk_mq_stop_hw_queue
);
651 void blk_mq_stop_hw_queues(struct request_queue
*q
)
653 struct blk_mq_hw_ctx
*hctx
;
656 queue_for_each_hw_ctx(q
, hctx
, i
)
657 blk_mq_stop_hw_queue(hctx
);
659 EXPORT_SYMBOL(blk_mq_stop_hw_queues
);
661 void blk_mq_start_hw_queue(struct blk_mq_hw_ctx
*hctx
)
663 clear_bit(BLK_MQ_S_STOPPED
, &hctx
->state
);
664 __blk_mq_run_hw_queue(hctx
);
666 EXPORT_SYMBOL(blk_mq_start_hw_queue
);
668 void blk_mq_start_stopped_hw_queues(struct request_queue
*q
)
670 struct blk_mq_hw_ctx
*hctx
;
673 queue_for_each_hw_ctx(q
, hctx
, i
) {
674 if (!test_bit(BLK_MQ_S_STOPPED
, &hctx
->state
))
677 clear_bit(BLK_MQ_S_STOPPED
, &hctx
->state
);
678 blk_mq_run_hw_queue(hctx
, true);
681 EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues
);
683 static void blk_mq_work_fn(struct work_struct
*work
)
685 struct blk_mq_hw_ctx
*hctx
;
687 hctx
= container_of(work
, struct blk_mq_hw_ctx
, delayed_work
.work
);
688 __blk_mq_run_hw_queue(hctx
);
691 static void __blk_mq_insert_request(struct blk_mq_hw_ctx
*hctx
,
692 struct request
*rq
, bool at_head
)
694 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
;
696 trace_block_rq_insert(hctx
->queue
, rq
);
699 list_add(&rq
->queuelist
, &ctx
->rq_list
);
701 list_add_tail(&rq
->queuelist
, &ctx
->rq_list
);
702 blk_mq_hctx_mark_pending(hctx
, ctx
);
705 * We do this early, to ensure we are on the right CPU.
707 blk_mq_add_timer(rq
);
710 void blk_mq_insert_request(struct request
*rq
, bool at_head
, bool run_queue
,
713 struct request_queue
*q
= rq
->q
;
714 struct blk_mq_hw_ctx
*hctx
;
715 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
, *current_ctx
;
717 current_ctx
= blk_mq_get_ctx(q
);
718 if (!cpu_online(ctx
->cpu
))
719 rq
->mq_ctx
= ctx
= current_ctx
;
721 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
723 if (rq
->cmd_flags
& (REQ_FLUSH
| REQ_FUA
) &&
724 !(rq
->cmd_flags
& (REQ_FLUSH_SEQ
))) {
725 blk_insert_flush(rq
);
727 spin_lock(&ctx
->lock
);
728 __blk_mq_insert_request(hctx
, rq
, at_head
);
729 spin_unlock(&ctx
->lock
);
732 blk_mq_put_ctx(current_ctx
);
735 blk_mq_run_hw_queue(hctx
, async
);
738 static void blk_mq_insert_requests(struct request_queue
*q
,
739 struct blk_mq_ctx
*ctx
,
740 struct list_head
*list
,
745 struct blk_mq_hw_ctx
*hctx
;
746 struct blk_mq_ctx
*current_ctx
;
748 trace_block_unplug(q
, depth
, !from_schedule
);
750 current_ctx
= blk_mq_get_ctx(q
);
752 if (!cpu_online(ctx
->cpu
))
754 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
757 * preemption doesn't flush plug list, so it's possible ctx->cpu is
760 spin_lock(&ctx
->lock
);
761 while (!list_empty(list
)) {
764 rq
= list_first_entry(list
, struct request
, queuelist
);
765 list_del_init(&rq
->queuelist
);
767 __blk_mq_insert_request(hctx
, rq
, false);
769 spin_unlock(&ctx
->lock
);
771 blk_mq_put_ctx(current_ctx
);
773 blk_mq_run_hw_queue(hctx
, from_schedule
);
776 static int plug_ctx_cmp(void *priv
, struct list_head
*a
, struct list_head
*b
)
778 struct request
*rqa
= container_of(a
, struct request
, queuelist
);
779 struct request
*rqb
= container_of(b
, struct request
, queuelist
);
781 return !(rqa
->mq_ctx
< rqb
->mq_ctx
||
782 (rqa
->mq_ctx
== rqb
->mq_ctx
&&
783 blk_rq_pos(rqa
) < blk_rq_pos(rqb
)));
786 void blk_mq_flush_plug_list(struct blk_plug
*plug
, bool from_schedule
)
788 struct blk_mq_ctx
*this_ctx
;
789 struct request_queue
*this_q
;
795 list_splice_init(&plug
->mq_list
, &list
);
797 list_sort(NULL
, &list
, plug_ctx_cmp
);
803 while (!list_empty(&list
)) {
804 rq
= list_entry_rq(list
.next
);
805 list_del_init(&rq
->queuelist
);
807 if (rq
->mq_ctx
!= this_ctx
) {
809 blk_mq_insert_requests(this_q
, this_ctx
,
814 this_ctx
= rq
->mq_ctx
;
820 list_add_tail(&rq
->queuelist
, &ctx_list
);
824 * If 'this_ctx' is set, we know we have entries to complete
825 * on 'ctx_list'. Do those.
828 blk_mq_insert_requests(this_q
, this_ctx
, &ctx_list
, depth
,
833 static void blk_mq_bio_to_request(struct request
*rq
, struct bio
*bio
)
835 init_request_from_bio(rq
, bio
);
836 blk_account_io_start(rq
, 1);
839 static void blk_mq_make_request(struct request_queue
*q
, struct bio
*bio
)
841 struct blk_mq_hw_ctx
*hctx
;
842 struct blk_mq_ctx
*ctx
;
843 const int is_sync
= rw_is_sync(bio
->bi_rw
);
844 const int is_flush_fua
= bio
->bi_rw
& (REQ_FLUSH
| REQ_FUA
);
845 int rw
= bio_data_dir(bio
);
847 unsigned int use_plug
, request_count
= 0;
850 * If we have multiple hardware queues, just go directly to
851 * one of those for sync IO.
853 use_plug
= !is_flush_fua
&& ((q
->nr_hw_queues
== 1) || !is_sync
);
855 blk_queue_bounce(q
, &bio
);
857 if (bio_integrity_enabled(bio
) && bio_integrity_prep(bio
)) {
858 bio_endio(bio
, -EIO
);
862 if (use_plug
&& blk_attempt_plug_merge(q
, bio
, &request_count
))
865 if (blk_mq_queue_enter(q
)) {
866 bio_endio(bio
, -EIO
);
870 ctx
= blk_mq_get_ctx(q
);
871 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
875 trace_block_getrq(q
, bio
, rw
);
876 rq
= __blk_mq_alloc_request(hctx
, GFP_ATOMIC
, false);
878 blk_mq_rq_ctx_init(q
, ctx
, rq
, rw
);
881 trace_block_sleeprq(q
, bio
, rw
);
882 rq
= blk_mq_alloc_request_pinned(q
, rw
, __GFP_WAIT
|GFP_ATOMIC
,
885 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
890 if (unlikely(is_flush_fua
)) {
891 blk_mq_bio_to_request(rq
, bio
);
893 blk_insert_flush(rq
);
898 * A task plug currently exists. Since this is completely lockless,
899 * utilize that to temporarily store requests until the task is
900 * either done or scheduled away.
903 struct blk_plug
*plug
= current
->plug
;
906 blk_mq_bio_to_request(rq
, bio
);
907 if (list_empty(&plug
->mq_list
))
909 else if (request_count
>= BLK_MAX_REQUEST_COUNT
) {
910 spin_unlock(&ctx
->cpu_lock
);
911 blk_flush_plug_list(plug
, false);
912 spin_lock(&ctx
->cpu_lock
);
915 list_add_tail(&rq
->queuelist
, &plug
->mq_list
);
921 spin_lock(&ctx
->lock
);
923 if ((hctx
->flags
& BLK_MQ_F_SHOULD_MERGE
) &&
924 blk_mq_attempt_merge(q
, ctx
, bio
))
925 __blk_mq_free_request(hctx
, ctx
, rq
);
927 blk_mq_bio_to_request(rq
, bio
);
928 __blk_mq_insert_request(hctx
, rq
, false);
931 spin_unlock(&ctx
->lock
);
935 * For a SYNC request, send it to the hardware immediately. For an
936 * ASYNC request, just ensure that we run it later on. The latter
937 * allows for merging opportunities and more efficient dispatching.
940 blk_mq_run_hw_queue(hctx
, !is_sync
|| is_flush_fua
);
944 * Default mapping to a software queue, since we use one per CPU.
946 struct blk_mq_hw_ctx
*blk_mq_map_queue(struct request_queue
*q
, const int cpu
)
948 return q
->queue_hw_ctx
[q
->mq_map
[cpu
]];
950 EXPORT_SYMBOL(blk_mq_map_queue
);
952 struct blk_mq_hw_ctx
*blk_mq_alloc_single_hw_queue(struct blk_mq_reg
*reg
,
953 unsigned int hctx_index
)
955 return kmalloc_node(sizeof(struct blk_mq_hw_ctx
),
956 GFP_KERNEL
| __GFP_ZERO
, reg
->numa_node
);
958 EXPORT_SYMBOL(blk_mq_alloc_single_hw_queue
);
960 void blk_mq_free_single_hw_queue(struct blk_mq_hw_ctx
*hctx
,
961 unsigned int hctx_index
)
965 EXPORT_SYMBOL(blk_mq_free_single_hw_queue
);
967 static void blk_mq_hctx_notify(void *data
, unsigned long action
,
970 struct blk_mq_hw_ctx
*hctx
= data
;
971 struct blk_mq_ctx
*ctx
;
974 if (action
!= CPU_POST_DEAD
&& action
!= CPU_POST_DEAD
)
978 * Move ctx entries to new CPU, if this one is going away.
980 ctx
= __blk_mq_get_ctx(hctx
->queue
, cpu
);
982 spin_lock(&ctx
->lock
);
983 if (!list_empty(&ctx
->rq_list
)) {
984 list_splice_init(&ctx
->rq_list
, &tmp
);
985 clear_bit(ctx
->index_hw
, hctx
->ctx_map
);
987 spin_unlock(&ctx
->lock
);
988 __blk_mq_put_ctx(ctx
);
990 if (list_empty(&tmp
))
993 ctx
= blk_mq_get_ctx(hctx
->queue
);
994 spin_lock(&ctx
->lock
);
996 while (!list_empty(&tmp
)) {
999 rq
= list_first_entry(&tmp
, struct request
, queuelist
);
1001 list_move_tail(&rq
->queuelist
, &ctx
->rq_list
);
1004 blk_mq_hctx_mark_pending(hctx
, ctx
);
1006 spin_unlock(&ctx
->lock
);
1007 blk_mq_put_ctx(ctx
);
1010 static void blk_mq_init_hw_commands(struct blk_mq_hw_ctx
*hctx
,
1011 void (*init
)(void *, struct blk_mq_hw_ctx
*,
1012 struct request
*, unsigned int),
1017 for (i
= 0; i
< hctx
->queue_depth
; i
++) {
1018 struct request
*rq
= hctx
->rqs
[i
];
1020 init(data
, hctx
, rq
, i
);
1024 void blk_mq_init_commands(struct request_queue
*q
,
1025 void (*init
)(void *, struct blk_mq_hw_ctx
*,
1026 struct request
*, unsigned int),
1029 struct blk_mq_hw_ctx
*hctx
;
1032 queue_for_each_hw_ctx(q
, hctx
, i
)
1033 blk_mq_init_hw_commands(hctx
, init
, data
);
1035 EXPORT_SYMBOL(blk_mq_init_commands
);
1037 static void blk_mq_free_rq_map(struct blk_mq_hw_ctx
*hctx
)
1041 while (!list_empty(&hctx
->page_list
)) {
1042 page
= list_first_entry(&hctx
->page_list
, struct page
, lru
);
1043 list_del_init(&page
->lru
);
1044 __free_pages(page
, page
->private);
1050 blk_mq_free_tags(hctx
->tags
);
1053 static size_t order_to_size(unsigned int order
)
1055 size_t ret
= PAGE_SIZE
;
1063 static int blk_mq_init_rq_map(struct blk_mq_hw_ctx
*hctx
,
1064 unsigned int reserved_tags
, int node
)
1066 unsigned int i
, j
, entries_per_page
, max_order
= 4;
1067 size_t rq_size
, left
;
1069 INIT_LIST_HEAD(&hctx
->page_list
);
1071 hctx
->rqs
= kmalloc_node(hctx
->queue_depth
* sizeof(struct request
*),
1077 * rq_size is the size of the request plus driver payload, rounded
1078 * to the cacheline size
1080 rq_size
= round_up(sizeof(struct request
) + hctx
->cmd_size
,
1082 left
= rq_size
* hctx
->queue_depth
;
1084 for (i
= 0; i
< hctx
->queue_depth
;) {
1085 int this_order
= max_order
;
1090 while (left
< order_to_size(this_order
- 1) && this_order
)
1094 page
= alloc_pages_node(node
, GFP_KERNEL
, this_order
);
1099 if (order_to_size(this_order
) < rq_size
)
1106 page
->private = this_order
;
1107 list_add_tail(&page
->lru
, &hctx
->page_list
);
1109 p
= page_address(page
);
1110 entries_per_page
= order_to_size(this_order
) / rq_size
;
1111 to_do
= min(entries_per_page
, hctx
->queue_depth
- i
);
1112 left
-= to_do
* rq_size
;
1113 for (j
= 0; j
< to_do
; j
++) {
1115 blk_mq_rq_init(hctx
, hctx
->rqs
[i
]);
1121 if (i
< (reserved_tags
+ BLK_MQ_TAG_MIN
))
1123 else if (i
!= hctx
->queue_depth
) {
1124 hctx
->queue_depth
= i
;
1125 pr_warn("%s: queue depth set to %u because of low memory\n",
1129 hctx
->tags
= blk_mq_init_tags(hctx
->queue_depth
, reserved_tags
, node
);
1132 blk_mq_free_rq_map(hctx
);
1139 static int blk_mq_init_hw_queues(struct request_queue
*q
,
1140 struct blk_mq_reg
*reg
, void *driver_data
)
1142 struct blk_mq_hw_ctx
*hctx
;
1146 * Initialize hardware queues
1148 queue_for_each_hw_ctx(q
, hctx
, i
) {
1149 unsigned int num_maps
;
1152 node
= hctx
->numa_node
;
1153 if (node
== NUMA_NO_NODE
)
1154 node
= hctx
->numa_node
= reg
->numa_node
;
1156 INIT_DELAYED_WORK(&hctx
->delayed_work
, blk_mq_work_fn
);
1157 spin_lock_init(&hctx
->lock
);
1158 INIT_LIST_HEAD(&hctx
->dispatch
);
1160 hctx
->queue_num
= i
;
1161 hctx
->flags
= reg
->flags
;
1162 hctx
->queue_depth
= reg
->queue_depth
;
1163 hctx
->cmd_size
= reg
->cmd_size
;
1165 blk_mq_init_cpu_notifier(&hctx
->cpu_notifier
,
1166 blk_mq_hctx_notify
, hctx
);
1167 blk_mq_register_cpu_notifier(&hctx
->cpu_notifier
);
1169 if (blk_mq_init_rq_map(hctx
, reg
->reserved_tags
, node
))
1173 * Allocate space for all possible cpus to avoid allocation in
1176 hctx
->ctxs
= kmalloc_node(nr_cpu_ids
* sizeof(void *),
1181 num_maps
= ALIGN(nr_cpu_ids
, BITS_PER_LONG
) / BITS_PER_LONG
;
1182 hctx
->ctx_map
= kzalloc_node(num_maps
* sizeof(unsigned long),
1187 hctx
->nr_ctx_map
= num_maps
;
1190 if (reg
->ops
->init_hctx
&&
1191 reg
->ops
->init_hctx(hctx
, driver_data
, i
))
1195 if (i
== q
->nr_hw_queues
)
1201 queue_for_each_hw_ctx(q
, hctx
, j
) {
1205 if (reg
->ops
->exit_hctx
)
1206 reg
->ops
->exit_hctx(hctx
, j
);
1208 blk_mq_unregister_cpu_notifier(&hctx
->cpu_notifier
);
1209 blk_mq_free_rq_map(hctx
);
1216 static void blk_mq_init_cpu_queues(struct request_queue
*q
,
1217 unsigned int nr_hw_queues
)
1221 for_each_possible_cpu(i
) {
1222 struct blk_mq_ctx
*__ctx
= per_cpu_ptr(q
->queue_ctx
, i
);
1223 struct blk_mq_hw_ctx
*hctx
;
1225 memset(__ctx
, 0, sizeof(*__ctx
));
1227 spin_lock_init(&__ctx
->lock
);
1228 spin_lock_init(&__ctx
->cpu_lock
);
1229 INIT_LIST_HEAD(&__ctx
->rq_list
);
1232 /* If the cpu isn't online, the cpu is mapped to first hctx */
1233 hctx
= q
->mq_ops
->map_queue(q
, i
);
1240 * Set local node, IFF we have more than one hw queue. If
1241 * not, we remain on the home node of the device
1243 if (nr_hw_queues
> 1 && hctx
->numa_node
== NUMA_NO_NODE
)
1244 hctx
->numa_node
= cpu_to_node(i
);
1248 static void blk_mq_map_swqueue(struct request_queue
*q
)
1251 struct blk_mq_hw_ctx
*hctx
;
1252 struct blk_mq_ctx
*ctx
;
1254 queue_for_each_hw_ctx(q
, hctx
, i
) {
1259 * Map software to hardware queues
1261 queue_for_each_ctx(q
, ctx
, i
) {
1262 /* If the cpu isn't online, the cpu is mapped to first hctx */
1263 hctx
= q
->mq_ops
->map_queue(q
, i
);
1264 ctx
->index_hw
= hctx
->nr_ctx
;
1265 hctx
->ctxs
[hctx
->nr_ctx
++] = ctx
;
1269 struct request_queue
*blk_mq_init_queue(struct blk_mq_reg
*reg
,
1272 struct blk_mq_hw_ctx
**hctxs
;
1273 struct blk_mq_ctx
*ctx
;
1274 struct request_queue
*q
;
1277 if (!reg
->nr_hw_queues
||
1278 !reg
->ops
->queue_rq
|| !reg
->ops
->map_queue
||
1279 !reg
->ops
->alloc_hctx
|| !reg
->ops
->free_hctx
)
1280 return ERR_PTR(-EINVAL
);
1282 if (!reg
->queue_depth
)
1283 reg
->queue_depth
= BLK_MQ_MAX_DEPTH
;
1284 else if (reg
->queue_depth
> BLK_MQ_MAX_DEPTH
) {
1285 pr_err("blk-mq: queuedepth too large (%u)\n", reg
->queue_depth
);
1286 reg
->queue_depth
= BLK_MQ_MAX_DEPTH
;
1289 if (reg
->queue_depth
< (reg
->reserved_tags
+ BLK_MQ_TAG_MIN
))
1290 return ERR_PTR(-EINVAL
);
1292 ctx
= alloc_percpu(struct blk_mq_ctx
);
1294 return ERR_PTR(-ENOMEM
);
1296 hctxs
= kmalloc_node(reg
->nr_hw_queues
* sizeof(*hctxs
), GFP_KERNEL
,
1302 for (i
= 0; i
< reg
->nr_hw_queues
; i
++) {
1303 hctxs
[i
] = reg
->ops
->alloc_hctx(reg
, i
);
1307 hctxs
[i
]->numa_node
= NUMA_NO_NODE
;
1308 hctxs
[i
]->queue_num
= i
;
1311 q
= blk_alloc_queue_node(GFP_KERNEL
, reg
->numa_node
);
1315 q
->mq_map
= blk_mq_make_queue_map(reg
);
1319 setup_timer(&q
->timeout
, blk_mq_rq_timer
, (unsigned long) q
);
1320 blk_queue_rq_timeout(q
, 30000);
1322 q
->nr_queues
= nr_cpu_ids
;
1323 q
->nr_hw_queues
= reg
->nr_hw_queues
;
1326 q
->queue_hw_ctx
= hctxs
;
1328 q
->mq_ops
= reg
->ops
;
1329 q
->queue_flags
|= QUEUE_FLAG_MQ_DEFAULT
;
1331 q
->sg_reserved_size
= INT_MAX
;
1333 blk_queue_make_request(q
, blk_mq_make_request
);
1334 blk_queue_rq_timed_out(q
, reg
->ops
->timeout
);
1336 blk_queue_rq_timeout(q
, reg
->timeout
);
1338 if (reg
->ops
->complete
)
1339 blk_queue_softirq_done(q
, reg
->ops
->complete
);
1341 blk_mq_init_flush(q
);
1342 blk_mq_init_cpu_queues(q
, reg
->nr_hw_queues
);
1344 q
->flush_rq
= kzalloc(round_up(sizeof(struct request
) + reg
->cmd_size
,
1345 cache_line_size()), GFP_KERNEL
);
1349 if (blk_mq_init_hw_queues(q
, reg
, driver_data
))
1352 blk_mq_map_swqueue(q
);
1354 mutex_lock(&all_q_mutex
);
1355 list_add_tail(&q
->all_q_node
, &all_q_list
);
1356 mutex_unlock(&all_q_mutex
);
1365 blk_cleanup_queue(q
);
1367 for (i
= 0; i
< reg
->nr_hw_queues
; i
++) {
1370 reg
->ops
->free_hctx(hctxs
[i
], i
);
1375 return ERR_PTR(-ENOMEM
);
1377 EXPORT_SYMBOL(blk_mq_init_queue
);
1379 void blk_mq_free_queue(struct request_queue
*q
)
1381 struct blk_mq_hw_ctx
*hctx
;
1384 queue_for_each_hw_ctx(q
, hctx
, i
) {
1385 kfree(hctx
->ctx_map
);
1387 blk_mq_free_rq_map(hctx
);
1388 blk_mq_unregister_cpu_notifier(&hctx
->cpu_notifier
);
1389 if (q
->mq_ops
->exit_hctx
)
1390 q
->mq_ops
->exit_hctx(hctx
, i
);
1391 q
->mq_ops
->free_hctx(hctx
, i
);
1394 free_percpu(q
->queue_ctx
);
1395 kfree(q
->queue_hw_ctx
);
1398 q
->queue_ctx
= NULL
;
1399 q
->queue_hw_ctx
= NULL
;
1402 mutex_lock(&all_q_mutex
);
1403 list_del_init(&q
->all_q_node
);
1404 mutex_unlock(&all_q_mutex
);
1407 /* Basically redo blk_mq_init_queue with queue frozen */
1408 static void blk_mq_queue_reinit(struct request_queue
*q
)
1410 blk_mq_freeze_queue(q
);
1412 blk_mq_update_queue_map(q
->mq_map
, q
->nr_hw_queues
);
1415 * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
1416 * we should change hctx numa_node according to new topology (this
1417 * involves free and re-allocate memory, worthy doing?)
1420 blk_mq_map_swqueue(q
);
1422 blk_mq_unfreeze_queue(q
);
1425 static int blk_mq_queue_reinit_notify(struct notifier_block
*nb
,
1426 unsigned long action
, void *hcpu
)
1428 struct request_queue
*q
;
1431 * Before new mapping is established, hotadded cpu might already start
1432 * handling requests. This doesn't break anything as we map offline
1433 * CPUs to first hardware queue. We will re-init queue below to get
1436 if (action
!= CPU_DEAD
&& action
!= CPU_DEAD_FROZEN
&&
1437 action
!= CPU_ONLINE
&& action
!= CPU_ONLINE_FROZEN
)
1440 mutex_lock(&all_q_mutex
);
1441 list_for_each_entry(q
, &all_q_list
, all_q_node
)
1442 blk_mq_queue_reinit(q
);
1443 mutex_unlock(&all_q_mutex
);
1447 static int __init
blk_mq_init(void)
1451 /* Must be called after percpu_counter_hotcpu_callback() */
1452 hotcpu_notifier(blk_mq_queue_reinit_notify
, -10);
1456 subsys_initcall(blk_mq_init
);