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_rq(struct blk_mq_hw_ctx
*hctx
, gfp_t gfp
,
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(struct blk_mq_hw_ctx
*hctx
,
197 gfp_t gfp
, bool reserved
,
201 bool is_flush
= false;
203 * flush need allocate a request, leave at least one request for
204 * non-flush IO to avoid deadlock
206 if ((rw
& REQ_FLUSH
) && !(rw
& REQ_FLUSH_SEQ
)) {
207 if (atomic_inc_return(&hctx
->pending_flush
) >=
208 hctx
->queue_depth
- hctx
->reserved_tags
- 1) {
209 atomic_dec(&hctx
->pending_flush
);
214 req
= blk_mq_alloc_rq(hctx
, gfp
, reserved
);
215 if (!req
&& is_flush
)
216 atomic_dec(&hctx
->pending_flush
);
220 static struct request
*blk_mq_alloc_request_pinned(struct request_queue
*q
,
227 struct blk_mq_ctx
*ctx
= blk_mq_get_ctx(q
);
228 struct blk_mq_hw_ctx
*hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
230 rq
= __blk_mq_alloc_request(hctx
, gfp
& ~__GFP_WAIT
, reserved
, rw
);
232 blk_mq_rq_ctx_init(q
, ctx
, rq
, rw
);
237 if (!(gfp
& __GFP_WAIT
))
240 __blk_mq_run_hw_queue(hctx
);
241 blk_mq_wait_for_tags(hctx
->tags
);
247 struct request
*blk_mq_alloc_request(struct request_queue
*q
, int rw
,
248 gfp_t gfp
, bool reserved
)
252 if (blk_mq_queue_enter(q
))
255 rq
= blk_mq_alloc_request_pinned(q
, rw
, gfp
, reserved
);
257 blk_mq_put_ctx(rq
->mq_ctx
);
261 struct request
*blk_mq_alloc_reserved_request(struct request_queue
*q
, int rw
,
266 if (blk_mq_queue_enter(q
))
269 rq
= blk_mq_alloc_request_pinned(q
, rw
, gfp
, true);
271 blk_mq_put_ctx(rq
->mq_ctx
);
274 EXPORT_SYMBOL(blk_mq_alloc_reserved_request
);
277 * Re-init and set pdu, if we have it
279 static void blk_mq_rq_init(struct blk_mq_hw_ctx
*hctx
, struct request
*rq
)
281 blk_rq_init(hctx
->queue
, rq
);
284 rq
->special
= blk_mq_rq_to_pdu(rq
);
287 static void __blk_mq_free_request(struct blk_mq_hw_ctx
*hctx
,
288 struct blk_mq_ctx
*ctx
, struct request
*rq
)
290 const int tag
= rq
->tag
;
291 struct request_queue
*q
= rq
->q
;
293 if ((rq
->cmd_flags
& REQ_FLUSH
) && !(rq
->cmd_flags
& REQ_FLUSH_SEQ
))
294 atomic_dec(&hctx
->pending_flush
);
296 blk_mq_rq_init(hctx
, rq
);
297 blk_mq_put_tag(hctx
->tags
, tag
);
299 blk_mq_queue_exit(q
);
302 void blk_mq_free_request(struct request
*rq
)
304 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
;
305 struct blk_mq_hw_ctx
*hctx
;
306 struct request_queue
*q
= rq
->q
;
308 ctx
->rq_completed
[rq_is_sync(rq
)]++;
310 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
311 __blk_mq_free_request(hctx
, ctx
, rq
);
314 static void blk_mq_bio_endio(struct request
*rq
, struct bio
*bio
, int error
)
317 clear_bit(BIO_UPTODATE
, &bio
->bi_flags
);
318 else if (!test_bit(BIO_UPTODATE
, &bio
->bi_flags
))
321 if (unlikely(rq
->cmd_flags
& REQ_QUIET
))
322 set_bit(BIO_QUIET
, &bio
->bi_flags
);
324 /* don't actually finish bio if it's part of flush sequence */
325 if (!(rq
->cmd_flags
& REQ_FLUSH_SEQ
))
326 bio_endio(bio
, error
);
329 void blk_mq_complete_request(struct request
*rq
, int error
)
331 struct bio
*bio
= rq
->bio
;
332 unsigned int bytes
= 0;
334 trace_block_rq_complete(rq
->q
, rq
);
337 struct bio
*next
= bio
->bi_next
;
340 bytes
+= bio
->bi_iter
.bi_size
;
341 blk_mq_bio_endio(rq
, bio
, error
);
345 blk_account_io_completion(rq
, bytes
);
347 blk_account_io_done(rq
);
350 rq
->end_io(rq
, error
);
352 blk_mq_free_request(rq
);
355 void __blk_mq_end_io(struct request
*rq
, int error
)
357 if (!blk_mark_rq_complete(rq
))
358 blk_mq_complete_request(rq
, error
);
361 static void blk_mq_end_io_remote(void *data
)
363 struct request
*rq
= data
;
365 __blk_mq_end_io(rq
, rq
->errors
);
369 * End IO on this request on a multiqueue enabled driver. We'll either do
370 * it directly inline, or punt to a local IPI handler on the matching
373 void blk_mq_end_io(struct request
*rq
, int error
)
375 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
;
378 if (!ctx
->ipi_redirect
)
379 return __blk_mq_end_io(rq
, error
);
382 if (cpu
!= ctx
->cpu
&& cpu_online(ctx
->cpu
)) {
384 rq
->csd
.func
= blk_mq_end_io_remote
;
387 __smp_call_function_single(ctx
->cpu
, &rq
->csd
, 0);
389 __blk_mq_end_io(rq
, error
);
393 EXPORT_SYMBOL(blk_mq_end_io
);
395 static void blk_mq_start_request(struct request
*rq
)
397 struct request_queue
*q
= rq
->q
;
399 trace_block_rq_issue(q
, rq
);
402 * Just mark start time and set the started bit. Due to memory
403 * ordering, we know we'll see the correct deadline as long as
404 * REQ_ATOMIC_STARTED is seen.
406 rq
->deadline
= jiffies
+ q
->rq_timeout
;
407 set_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
);
410 static void blk_mq_requeue_request(struct request
*rq
)
412 struct request_queue
*q
= rq
->q
;
414 trace_block_rq_requeue(q
, rq
);
415 clear_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
);
418 struct blk_mq_timeout_data
{
419 struct blk_mq_hw_ctx
*hctx
;
421 unsigned int *next_set
;
424 static void blk_mq_timeout_check(void *__data
, unsigned long *free_tags
)
426 struct blk_mq_timeout_data
*data
= __data
;
427 struct blk_mq_hw_ctx
*hctx
= data
->hctx
;
430 /* It may not be in flight yet (this is where
431 * the REQ_ATOMIC_STARTED flag comes in). The requests are
432 * statically allocated, so we know it's always safe to access the
433 * memory associated with a bit offset into ->rqs[].
439 tag
= find_next_zero_bit(free_tags
, hctx
->queue_depth
, tag
);
440 if (tag
>= hctx
->queue_depth
)
443 rq
= hctx
->rqs
[tag
++];
445 if (!test_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
))
448 blk_rq_check_expired(rq
, data
->next
, data
->next_set
);
452 static void blk_mq_hw_ctx_check_timeout(struct blk_mq_hw_ctx
*hctx
,
454 unsigned int *next_set
)
456 struct blk_mq_timeout_data data
= {
459 .next_set
= next_set
,
463 * Ask the tagging code to iterate busy requests, so we can
464 * check them for timeout.
466 blk_mq_tag_busy_iter(hctx
->tags
, blk_mq_timeout_check
, &data
);
469 static void blk_mq_rq_timer(unsigned long data
)
471 struct request_queue
*q
= (struct request_queue
*) data
;
472 struct blk_mq_hw_ctx
*hctx
;
473 unsigned long next
= 0;
476 queue_for_each_hw_ctx(q
, hctx
, i
)
477 blk_mq_hw_ctx_check_timeout(hctx
, &next
, &next_set
);
480 mod_timer(&q
->timeout
, round_jiffies_up(next
));
484 * Reverse check our software queue for entries that we could potentially
485 * merge with. Currently includes a hand-wavy stop count of 8, to not spend
486 * too much time checking for merges.
488 static bool blk_mq_attempt_merge(struct request_queue
*q
,
489 struct blk_mq_ctx
*ctx
, struct bio
*bio
)
494 list_for_each_entry_reverse(rq
, &ctx
->rq_list
, queuelist
) {
500 if (!blk_rq_merge_ok(rq
, bio
))
503 el_ret
= blk_try_merge(rq
, bio
);
504 if (el_ret
== ELEVATOR_BACK_MERGE
) {
505 if (bio_attempt_back_merge(q
, rq
, bio
)) {
510 } else if (el_ret
== ELEVATOR_FRONT_MERGE
) {
511 if (bio_attempt_front_merge(q
, rq
, bio
)) {
522 void blk_mq_add_timer(struct request
*rq
)
524 __blk_add_timer(rq
, NULL
);
528 * Run this hardware queue, pulling any software queues mapped to it in.
529 * Note that this function currently has various problems around ordering
530 * of IO. In particular, we'd like FIFO behaviour on handling existing
531 * items on the hctx->dispatch list. Ignore that for now.
533 static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx
*hctx
)
535 struct request_queue
*q
= hctx
->queue
;
536 struct blk_mq_ctx
*ctx
;
541 if (unlikely(test_bit(BLK_MQ_S_STOPPED
, &hctx
->flags
)))
547 * Touch any software queue that has pending entries.
549 for_each_set_bit(bit
, hctx
->ctx_map
, hctx
->nr_ctx
) {
550 clear_bit(bit
, hctx
->ctx_map
);
551 ctx
= hctx
->ctxs
[bit
];
552 BUG_ON(bit
!= ctx
->index_hw
);
554 spin_lock(&ctx
->lock
);
555 list_splice_tail_init(&ctx
->rq_list
, &rq_list
);
556 spin_unlock(&ctx
->lock
);
560 * If we have previous entries on our dispatch list, grab them
561 * and stuff them at the front for more fair dispatch.
563 if (!list_empty_careful(&hctx
->dispatch
)) {
564 spin_lock(&hctx
->lock
);
565 if (!list_empty(&hctx
->dispatch
))
566 list_splice_init(&hctx
->dispatch
, &rq_list
);
567 spin_unlock(&hctx
->lock
);
571 * Delete and return all entries from our dispatch list
576 * Now process all the entries, sending them to the driver.
578 while (!list_empty(&rq_list
)) {
581 rq
= list_first_entry(&rq_list
, struct request
, queuelist
);
582 list_del_init(&rq
->queuelist
);
583 blk_mq_start_request(rq
);
585 if (q
->dma_drain_size
&& blk_rq_bytes(rq
)) {
587 * make sure space for the drain appears we
588 * know we can do this because max_hw_segments
589 * has been adjusted to be one fewer than the
592 rq
->nr_phys_segments
++;
596 * Last request in the series. Flag it as such, this
597 * enables drivers to know when IO should be kicked off,
598 * if they don't do it on a per-request basis.
600 * Note: the flag isn't the only condition drivers
601 * should do kick off. If drive is busy, the last
602 * request might not have the bit set.
604 if (list_empty(&rq_list
))
605 rq
->cmd_flags
|= REQ_END
;
607 ret
= q
->mq_ops
->queue_rq(hctx
, rq
);
609 case BLK_MQ_RQ_QUEUE_OK
:
612 case BLK_MQ_RQ_QUEUE_BUSY
:
614 * FIXME: we should have a mechanism to stop the queue
615 * like blk_stop_queue, otherwise we will waste cpu
618 list_add(&rq
->queuelist
, &rq_list
);
619 blk_mq_requeue_request(rq
);
622 pr_err("blk-mq: bad return on queue: %d\n", ret
);
624 case BLK_MQ_RQ_QUEUE_ERROR
:
625 blk_mq_end_io(rq
, rq
->errors
);
629 if (ret
== BLK_MQ_RQ_QUEUE_BUSY
)
634 hctx
->dispatched
[0]++;
635 else if (queued
< (1 << (BLK_MQ_MAX_DISPATCH_ORDER
- 1)))
636 hctx
->dispatched
[ilog2(queued
) + 1]++;
639 * Any items that need requeuing? Stuff them into hctx->dispatch,
640 * that is where we will continue on next queue run.
642 if (!list_empty(&rq_list
)) {
643 spin_lock(&hctx
->lock
);
644 list_splice(&rq_list
, &hctx
->dispatch
);
645 spin_unlock(&hctx
->lock
);
649 void blk_mq_run_hw_queue(struct blk_mq_hw_ctx
*hctx
, bool async
)
651 if (unlikely(test_bit(BLK_MQ_S_STOPPED
, &hctx
->flags
)))
655 __blk_mq_run_hw_queue(hctx
);
657 struct request_queue
*q
= hctx
->queue
;
659 kblockd_schedule_delayed_work(q
, &hctx
->delayed_work
, 0);
663 void blk_mq_run_queues(struct request_queue
*q
, bool async
)
665 struct blk_mq_hw_ctx
*hctx
;
668 queue_for_each_hw_ctx(q
, hctx
, i
) {
669 if ((!blk_mq_hctx_has_pending(hctx
) &&
670 list_empty_careful(&hctx
->dispatch
)) ||
671 test_bit(BLK_MQ_S_STOPPED
, &hctx
->flags
))
674 blk_mq_run_hw_queue(hctx
, async
);
677 EXPORT_SYMBOL(blk_mq_run_queues
);
679 void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx
*hctx
)
681 cancel_delayed_work(&hctx
->delayed_work
);
682 set_bit(BLK_MQ_S_STOPPED
, &hctx
->state
);
684 EXPORT_SYMBOL(blk_mq_stop_hw_queue
);
686 void blk_mq_stop_hw_queues(struct request_queue
*q
)
688 struct blk_mq_hw_ctx
*hctx
;
691 queue_for_each_hw_ctx(q
, hctx
, i
)
692 blk_mq_stop_hw_queue(hctx
);
694 EXPORT_SYMBOL(blk_mq_stop_hw_queues
);
696 void blk_mq_start_hw_queue(struct blk_mq_hw_ctx
*hctx
)
698 clear_bit(BLK_MQ_S_STOPPED
, &hctx
->state
);
699 __blk_mq_run_hw_queue(hctx
);
701 EXPORT_SYMBOL(blk_mq_start_hw_queue
);
703 void blk_mq_start_stopped_hw_queues(struct request_queue
*q
)
705 struct blk_mq_hw_ctx
*hctx
;
708 queue_for_each_hw_ctx(q
, hctx
, i
) {
709 if (!test_bit(BLK_MQ_S_STOPPED
, &hctx
->state
))
712 clear_bit(BLK_MQ_S_STOPPED
, &hctx
->state
);
713 blk_mq_run_hw_queue(hctx
, true);
716 EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues
);
718 static void blk_mq_work_fn(struct work_struct
*work
)
720 struct blk_mq_hw_ctx
*hctx
;
722 hctx
= container_of(work
, struct blk_mq_hw_ctx
, delayed_work
.work
);
723 __blk_mq_run_hw_queue(hctx
);
726 static void __blk_mq_insert_request(struct blk_mq_hw_ctx
*hctx
,
727 struct request
*rq
, bool at_head
)
729 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
;
731 trace_block_rq_insert(hctx
->queue
, rq
);
734 list_add(&rq
->queuelist
, &ctx
->rq_list
);
736 list_add_tail(&rq
->queuelist
, &ctx
->rq_list
);
737 blk_mq_hctx_mark_pending(hctx
, ctx
);
740 * We do this early, to ensure we are on the right CPU.
742 blk_mq_add_timer(rq
);
745 void blk_mq_insert_request(struct request_queue
*q
, struct request
*rq
,
746 bool at_head
, bool run_queue
)
748 struct blk_mq_hw_ctx
*hctx
;
749 struct blk_mq_ctx
*ctx
, *current_ctx
;
752 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
754 if (rq
->cmd_flags
& (REQ_FLUSH
| REQ_FUA
)) {
755 blk_insert_flush(rq
);
757 current_ctx
= blk_mq_get_ctx(q
);
759 if (!cpu_online(ctx
->cpu
)) {
761 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
764 spin_lock(&ctx
->lock
);
765 __blk_mq_insert_request(hctx
, rq
, at_head
);
766 spin_unlock(&ctx
->lock
);
768 blk_mq_put_ctx(current_ctx
);
772 __blk_mq_run_hw_queue(hctx
);
774 EXPORT_SYMBOL(blk_mq_insert_request
);
777 * This is a special version of blk_mq_insert_request to bypass FLUSH request
778 * check. Should only be used internally.
780 void blk_mq_run_request(struct request
*rq
, bool run_queue
, bool async
)
782 struct request_queue
*q
= rq
->q
;
783 struct blk_mq_hw_ctx
*hctx
;
784 struct blk_mq_ctx
*ctx
, *current_ctx
;
786 current_ctx
= blk_mq_get_ctx(q
);
789 if (!cpu_online(ctx
->cpu
)) {
793 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
795 /* ctx->cpu might be offline */
796 spin_lock(&ctx
->lock
);
797 __blk_mq_insert_request(hctx
, rq
, false);
798 spin_unlock(&ctx
->lock
);
800 blk_mq_put_ctx(current_ctx
);
803 blk_mq_run_hw_queue(hctx
, async
);
806 static void blk_mq_insert_requests(struct request_queue
*q
,
807 struct blk_mq_ctx
*ctx
,
808 struct list_head
*list
,
813 struct blk_mq_hw_ctx
*hctx
;
814 struct blk_mq_ctx
*current_ctx
;
816 trace_block_unplug(q
, depth
, !from_schedule
);
818 current_ctx
= blk_mq_get_ctx(q
);
820 if (!cpu_online(ctx
->cpu
))
822 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
825 * preemption doesn't flush plug list, so it's possible ctx->cpu is
828 spin_lock(&ctx
->lock
);
829 while (!list_empty(list
)) {
832 rq
= list_first_entry(list
, struct request
, queuelist
);
833 list_del_init(&rq
->queuelist
);
835 __blk_mq_insert_request(hctx
, rq
, false);
837 spin_unlock(&ctx
->lock
);
839 blk_mq_put_ctx(current_ctx
);
841 blk_mq_run_hw_queue(hctx
, from_schedule
);
844 static int plug_ctx_cmp(void *priv
, struct list_head
*a
, struct list_head
*b
)
846 struct request
*rqa
= container_of(a
, struct request
, queuelist
);
847 struct request
*rqb
= container_of(b
, struct request
, queuelist
);
849 return !(rqa
->mq_ctx
< rqb
->mq_ctx
||
850 (rqa
->mq_ctx
== rqb
->mq_ctx
&&
851 blk_rq_pos(rqa
) < blk_rq_pos(rqb
)));
854 void blk_mq_flush_plug_list(struct blk_plug
*plug
, bool from_schedule
)
856 struct blk_mq_ctx
*this_ctx
;
857 struct request_queue
*this_q
;
863 list_splice_init(&plug
->mq_list
, &list
);
865 list_sort(NULL
, &list
, plug_ctx_cmp
);
871 while (!list_empty(&list
)) {
872 rq
= list_entry_rq(list
.next
);
873 list_del_init(&rq
->queuelist
);
875 if (rq
->mq_ctx
!= this_ctx
) {
877 blk_mq_insert_requests(this_q
, this_ctx
,
882 this_ctx
= rq
->mq_ctx
;
888 list_add_tail(&rq
->queuelist
, &ctx_list
);
892 * If 'this_ctx' is set, we know we have entries to complete
893 * on 'ctx_list'. Do those.
896 blk_mq_insert_requests(this_q
, this_ctx
, &ctx_list
, depth
,
901 static void blk_mq_bio_to_request(struct request
*rq
, struct bio
*bio
)
903 init_request_from_bio(rq
, bio
);
904 blk_account_io_start(rq
, 1);
907 static void blk_mq_make_request(struct request_queue
*q
, struct bio
*bio
)
909 struct blk_mq_hw_ctx
*hctx
;
910 struct blk_mq_ctx
*ctx
;
911 const int is_sync
= rw_is_sync(bio
->bi_rw
);
912 const int is_flush_fua
= bio
->bi_rw
& (REQ_FLUSH
| REQ_FUA
);
913 int rw
= bio_data_dir(bio
);
915 unsigned int use_plug
, request_count
= 0;
918 * If we have multiple hardware queues, just go directly to
919 * one of those for sync IO.
921 use_plug
= !is_flush_fua
&& ((q
->nr_hw_queues
== 1) || !is_sync
);
923 blk_queue_bounce(q
, &bio
);
925 if (bio_integrity_enabled(bio
) && bio_integrity_prep(bio
)) {
926 bio_endio(bio
, -EIO
);
930 if (use_plug
&& blk_attempt_plug_merge(q
, bio
, &request_count
))
933 if (blk_mq_queue_enter(q
)) {
934 bio_endio(bio
, -EIO
);
938 ctx
= blk_mq_get_ctx(q
);
939 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
941 trace_block_getrq(q
, bio
, rw
);
942 rq
= __blk_mq_alloc_request(hctx
, GFP_ATOMIC
, false, bio
->bi_rw
);
944 blk_mq_rq_ctx_init(q
, ctx
, rq
, bio
->bi_rw
);
947 trace_block_sleeprq(q
, bio
, rw
);
948 rq
= blk_mq_alloc_request_pinned(q
, bio
->bi_rw
,
949 __GFP_WAIT
|GFP_ATOMIC
, false);
951 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
956 if (unlikely(is_flush_fua
)) {
957 blk_mq_bio_to_request(rq
, bio
);
959 blk_insert_flush(rq
);
964 * A task plug currently exists. Since this is completely lockless,
965 * utilize that to temporarily store requests until the task is
966 * either done or scheduled away.
969 struct blk_plug
*plug
= current
->plug
;
972 blk_mq_bio_to_request(rq
, bio
);
973 if (list_empty(&plug
->mq_list
))
975 else if (request_count
>= BLK_MAX_REQUEST_COUNT
) {
976 blk_flush_plug_list(plug
, false);
979 list_add_tail(&rq
->queuelist
, &plug
->mq_list
);
985 spin_lock(&ctx
->lock
);
987 if ((hctx
->flags
& BLK_MQ_F_SHOULD_MERGE
) &&
988 blk_mq_attempt_merge(q
, ctx
, bio
))
989 __blk_mq_free_request(hctx
, ctx
, rq
);
991 blk_mq_bio_to_request(rq
, bio
);
992 __blk_mq_insert_request(hctx
, rq
, false);
995 spin_unlock(&ctx
->lock
);
999 * For a SYNC request, send it to the hardware immediately. For an
1000 * ASYNC request, just ensure that we run it later on. The latter
1001 * allows for merging opportunities and more efficient dispatching.
1004 blk_mq_run_hw_queue(hctx
, !is_sync
|| is_flush_fua
);
1008 * Default mapping to a software queue, since we use one per CPU.
1010 struct blk_mq_hw_ctx
*blk_mq_map_queue(struct request_queue
*q
, const int cpu
)
1012 return q
->queue_hw_ctx
[q
->mq_map
[cpu
]];
1014 EXPORT_SYMBOL(blk_mq_map_queue
);
1016 struct blk_mq_hw_ctx
*blk_mq_alloc_single_hw_queue(struct blk_mq_reg
*reg
,
1017 unsigned int hctx_index
)
1019 return kmalloc_node(sizeof(struct blk_mq_hw_ctx
),
1020 GFP_KERNEL
| __GFP_ZERO
, reg
->numa_node
);
1022 EXPORT_SYMBOL(blk_mq_alloc_single_hw_queue
);
1024 void blk_mq_free_single_hw_queue(struct blk_mq_hw_ctx
*hctx
,
1025 unsigned int hctx_index
)
1029 EXPORT_SYMBOL(blk_mq_free_single_hw_queue
);
1031 static void blk_mq_hctx_notify(void *data
, unsigned long action
,
1034 struct blk_mq_hw_ctx
*hctx
= data
;
1035 struct blk_mq_ctx
*ctx
;
1038 if (action
!= CPU_DEAD
&& action
!= CPU_DEAD_FROZEN
)
1042 * Move ctx entries to new CPU, if this one is going away.
1044 ctx
= __blk_mq_get_ctx(hctx
->queue
, cpu
);
1046 spin_lock(&ctx
->lock
);
1047 if (!list_empty(&ctx
->rq_list
)) {
1048 list_splice_init(&ctx
->rq_list
, &tmp
);
1049 clear_bit(ctx
->index_hw
, hctx
->ctx_map
);
1051 spin_unlock(&ctx
->lock
);
1053 if (list_empty(&tmp
))
1056 ctx
= blk_mq_get_ctx(hctx
->queue
);
1057 spin_lock(&ctx
->lock
);
1059 while (!list_empty(&tmp
)) {
1062 rq
= list_first_entry(&tmp
, struct request
, queuelist
);
1064 list_move_tail(&rq
->queuelist
, &ctx
->rq_list
);
1067 blk_mq_hctx_mark_pending(hctx
, ctx
);
1069 spin_unlock(&ctx
->lock
);
1070 blk_mq_put_ctx(ctx
);
1073 static void blk_mq_init_hw_commands(struct blk_mq_hw_ctx
*hctx
,
1074 void (*init
)(void *, struct blk_mq_hw_ctx
*,
1075 struct request
*, unsigned int),
1080 for (i
= 0; i
< hctx
->queue_depth
; i
++) {
1081 struct request
*rq
= hctx
->rqs
[i
];
1083 init(data
, hctx
, rq
, i
);
1087 void blk_mq_init_commands(struct request_queue
*q
,
1088 void (*init
)(void *, struct blk_mq_hw_ctx
*,
1089 struct request
*, unsigned int),
1092 struct blk_mq_hw_ctx
*hctx
;
1095 queue_for_each_hw_ctx(q
, hctx
, i
)
1096 blk_mq_init_hw_commands(hctx
, init
, data
);
1098 EXPORT_SYMBOL(blk_mq_init_commands
);
1100 static void blk_mq_free_rq_map(struct blk_mq_hw_ctx
*hctx
)
1104 while (!list_empty(&hctx
->page_list
)) {
1105 page
= list_first_entry(&hctx
->page_list
, struct page
, lru
);
1106 list_del_init(&page
->lru
);
1107 __free_pages(page
, page
->private);
1113 blk_mq_free_tags(hctx
->tags
);
1116 static size_t order_to_size(unsigned int order
)
1118 size_t ret
= PAGE_SIZE
;
1126 static int blk_mq_init_rq_map(struct blk_mq_hw_ctx
*hctx
,
1127 unsigned int reserved_tags
, int node
)
1129 unsigned int i
, j
, entries_per_page
, max_order
= 4;
1130 size_t rq_size
, left
;
1132 INIT_LIST_HEAD(&hctx
->page_list
);
1134 hctx
->rqs
= kmalloc_node(hctx
->queue_depth
* sizeof(struct request
*),
1140 * rq_size is the size of the request plus driver payload, rounded
1141 * to the cacheline size
1143 rq_size
= round_up(sizeof(struct request
) + hctx
->cmd_size
,
1145 left
= rq_size
* hctx
->queue_depth
;
1147 for (i
= 0; i
< hctx
->queue_depth
;) {
1148 int this_order
= max_order
;
1153 while (left
< order_to_size(this_order
- 1) && this_order
)
1157 page
= alloc_pages_node(node
, GFP_KERNEL
, this_order
);
1162 if (order_to_size(this_order
) < rq_size
)
1169 page
->private = this_order
;
1170 list_add_tail(&page
->lru
, &hctx
->page_list
);
1172 p
= page_address(page
);
1173 entries_per_page
= order_to_size(this_order
) / rq_size
;
1174 to_do
= min(entries_per_page
, hctx
->queue_depth
- i
);
1175 left
-= to_do
* rq_size
;
1176 for (j
= 0; j
< to_do
; j
++) {
1178 blk_mq_rq_init(hctx
, hctx
->rqs
[i
]);
1184 if (i
< (reserved_tags
+ BLK_MQ_TAG_MIN
))
1186 else if (i
!= hctx
->queue_depth
) {
1187 hctx
->queue_depth
= i
;
1188 pr_warn("%s: queue depth set to %u because of low memory\n",
1192 hctx
->tags
= blk_mq_init_tags(hctx
->queue_depth
, reserved_tags
, node
);
1195 blk_mq_free_rq_map(hctx
);
1202 static int blk_mq_init_hw_queues(struct request_queue
*q
,
1203 struct blk_mq_reg
*reg
, void *driver_data
)
1205 struct blk_mq_hw_ctx
*hctx
;
1209 * Initialize hardware queues
1211 queue_for_each_hw_ctx(q
, hctx
, i
) {
1212 unsigned int num_maps
;
1215 node
= hctx
->numa_node
;
1216 if (node
== NUMA_NO_NODE
)
1217 node
= hctx
->numa_node
= reg
->numa_node
;
1219 INIT_DELAYED_WORK(&hctx
->delayed_work
, blk_mq_work_fn
);
1220 spin_lock_init(&hctx
->lock
);
1221 INIT_LIST_HEAD(&hctx
->dispatch
);
1223 hctx
->queue_num
= i
;
1224 hctx
->flags
= reg
->flags
;
1225 hctx
->queue_depth
= reg
->queue_depth
;
1226 hctx
->reserved_tags
= reg
->reserved_tags
;
1227 hctx
->cmd_size
= reg
->cmd_size
;
1228 atomic_set(&hctx
->pending_flush
, 0);
1230 blk_mq_init_cpu_notifier(&hctx
->cpu_notifier
,
1231 blk_mq_hctx_notify
, hctx
);
1232 blk_mq_register_cpu_notifier(&hctx
->cpu_notifier
);
1234 if (blk_mq_init_rq_map(hctx
, reg
->reserved_tags
, node
))
1238 * Allocate space for all possible cpus to avoid allocation in
1241 hctx
->ctxs
= kmalloc_node(nr_cpu_ids
* sizeof(void *),
1246 num_maps
= ALIGN(nr_cpu_ids
, BITS_PER_LONG
) / BITS_PER_LONG
;
1247 hctx
->ctx_map
= kzalloc_node(num_maps
* sizeof(unsigned long),
1252 hctx
->nr_ctx_map
= num_maps
;
1255 if (reg
->ops
->init_hctx
&&
1256 reg
->ops
->init_hctx(hctx
, driver_data
, i
))
1260 if (i
== q
->nr_hw_queues
)
1266 queue_for_each_hw_ctx(q
, hctx
, j
) {
1270 if (reg
->ops
->exit_hctx
)
1271 reg
->ops
->exit_hctx(hctx
, j
);
1273 blk_mq_unregister_cpu_notifier(&hctx
->cpu_notifier
);
1274 blk_mq_free_rq_map(hctx
);
1281 static void blk_mq_init_cpu_queues(struct request_queue
*q
,
1282 unsigned int nr_hw_queues
)
1286 for_each_possible_cpu(i
) {
1287 struct blk_mq_ctx
*__ctx
= per_cpu_ptr(q
->queue_ctx
, i
);
1288 struct blk_mq_hw_ctx
*hctx
;
1290 memset(__ctx
, 0, sizeof(*__ctx
));
1292 spin_lock_init(&__ctx
->lock
);
1293 INIT_LIST_HEAD(&__ctx
->rq_list
);
1296 /* If the cpu isn't online, the cpu is mapped to first hctx */
1297 hctx
= q
->mq_ops
->map_queue(q
, i
);
1304 * Set local node, IFF we have more than one hw queue. If
1305 * not, we remain on the home node of the device
1307 if (nr_hw_queues
> 1 && hctx
->numa_node
== NUMA_NO_NODE
)
1308 hctx
->numa_node
= cpu_to_node(i
);
1312 static void blk_mq_map_swqueue(struct request_queue
*q
)
1315 struct blk_mq_hw_ctx
*hctx
;
1316 struct blk_mq_ctx
*ctx
;
1318 queue_for_each_hw_ctx(q
, hctx
, i
) {
1323 * Map software to hardware queues
1325 queue_for_each_ctx(q
, ctx
, i
) {
1326 /* If the cpu isn't online, the cpu is mapped to first hctx */
1327 hctx
= q
->mq_ops
->map_queue(q
, i
);
1328 ctx
->index_hw
= hctx
->nr_ctx
;
1329 hctx
->ctxs
[hctx
->nr_ctx
++] = ctx
;
1333 struct request_queue
*blk_mq_init_queue(struct blk_mq_reg
*reg
,
1336 struct blk_mq_hw_ctx
**hctxs
;
1337 struct blk_mq_ctx
*ctx
;
1338 struct request_queue
*q
;
1341 if (!reg
->nr_hw_queues
||
1342 !reg
->ops
->queue_rq
|| !reg
->ops
->map_queue
||
1343 !reg
->ops
->alloc_hctx
|| !reg
->ops
->free_hctx
)
1344 return ERR_PTR(-EINVAL
);
1346 if (!reg
->queue_depth
)
1347 reg
->queue_depth
= BLK_MQ_MAX_DEPTH
;
1348 else if (reg
->queue_depth
> BLK_MQ_MAX_DEPTH
) {
1349 pr_err("blk-mq: queuedepth too large (%u)\n", reg
->queue_depth
);
1350 reg
->queue_depth
= BLK_MQ_MAX_DEPTH
;
1353 if (reg
->queue_depth
< (reg
->reserved_tags
+ BLK_MQ_TAG_MIN
))
1354 return ERR_PTR(-EINVAL
);
1356 ctx
= alloc_percpu(struct blk_mq_ctx
);
1358 return ERR_PTR(-ENOMEM
);
1360 hctxs
= kmalloc_node(reg
->nr_hw_queues
* sizeof(*hctxs
), GFP_KERNEL
,
1366 for (i
= 0; i
< reg
->nr_hw_queues
; i
++) {
1367 hctxs
[i
] = reg
->ops
->alloc_hctx(reg
, i
);
1371 hctxs
[i
]->numa_node
= NUMA_NO_NODE
;
1372 hctxs
[i
]->queue_num
= i
;
1375 q
= blk_alloc_queue_node(GFP_KERNEL
, reg
->numa_node
);
1379 q
->mq_map
= blk_mq_make_queue_map(reg
);
1383 setup_timer(&q
->timeout
, blk_mq_rq_timer
, (unsigned long) q
);
1384 blk_queue_rq_timeout(q
, 30000);
1386 q
->nr_queues
= nr_cpu_ids
;
1387 q
->nr_hw_queues
= reg
->nr_hw_queues
;
1390 q
->queue_hw_ctx
= hctxs
;
1392 q
->mq_ops
= reg
->ops
;
1393 q
->queue_flags
|= QUEUE_FLAG_MQ_DEFAULT
;
1395 q
->sg_reserved_size
= INT_MAX
;
1397 blk_queue_make_request(q
, blk_mq_make_request
);
1398 blk_queue_rq_timed_out(q
, reg
->ops
->timeout
);
1400 blk_queue_rq_timeout(q
, reg
->timeout
);
1402 blk_mq_init_flush(q
);
1403 blk_mq_init_cpu_queues(q
, reg
->nr_hw_queues
);
1405 if (blk_mq_init_hw_queues(q
, reg
, driver_data
))
1408 blk_mq_map_swqueue(q
);
1410 mutex_lock(&all_q_mutex
);
1411 list_add_tail(&q
->all_q_node
, &all_q_list
);
1412 mutex_unlock(&all_q_mutex
);
1418 blk_cleanup_queue(q
);
1420 for (i
= 0; i
< reg
->nr_hw_queues
; i
++) {
1423 reg
->ops
->free_hctx(hctxs
[i
], i
);
1428 return ERR_PTR(-ENOMEM
);
1430 EXPORT_SYMBOL(blk_mq_init_queue
);
1432 void blk_mq_free_queue(struct request_queue
*q
)
1434 struct blk_mq_hw_ctx
*hctx
;
1437 queue_for_each_hw_ctx(q
, hctx
, i
) {
1438 kfree(hctx
->ctx_map
);
1440 blk_mq_free_rq_map(hctx
);
1441 blk_mq_unregister_cpu_notifier(&hctx
->cpu_notifier
);
1442 if (q
->mq_ops
->exit_hctx
)
1443 q
->mq_ops
->exit_hctx(hctx
, i
);
1444 q
->mq_ops
->free_hctx(hctx
, i
);
1447 free_percpu(q
->queue_ctx
);
1448 kfree(q
->queue_hw_ctx
);
1451 q
->queue_ctx
= NULL
;
1452 q
->queue_hw_ctx
= NULL
;
1455 mutex_lock(&all_q_mutex
);
1456 list_del_init(&q
->all_q_node
);
1457 mutex_unlock(&all_q_mutex
);
1460 /* Basically redo blk_mq_init_queue with queue frozen */
1461 static void blk_mq_queue_reinit(struct request_queue
*q
)
1463 blk_mq_freeze_queue(q
);
1465 blk_mq_update_queue_map(q
->mq_map
, q
->nr_hw_queues
);
1468 * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
1469 * we should change hctx numa_node according to new topology (this
1470 * involves free and re-allocate memory, worthy doing?)
1473 blk_mq_map_swqueue(q
);
1475 blk_mq_unfreeze_queue(q
);
1478 static int blk_mq_queue_reinit_notify(struct notifier_block
*nb
,
1479 unsigned long action
, void *hcpu
)
1481 struct request_queue
*q
;
1484 * Before new mapping is established, hotadded cpu might already start
1485 * handling requests. This doesn't break anything as we map offline
1486 * CPUs to first hardware queue. We will re-init queue below to get
1489 if (action
!= CPU_DEAD
&& action
!= CPU_DEAD_FROZEN
&&
1490 action
!= CPU_ONLINE
&& action
!= CPU_ONLINE_FROZEN
)
1493 mutex_lock(&all_q_mutex
);
1494 list_for_each_entry(q
, &all_q_list
, all_q_node
)
1495 blk_mq_queue_reinit(q
);
1496 mutex_unlock(&all_q_mutex
);
1500 static int __init
blk_mq_init(void)
1504 /* Must be called after percpu_counter_hotcpu_callback() */
1505 hotcpu_notifier(blk_mq_queue_reinit_notify
, -10);
1509 subsys_initcall(blk_mq_init
);